属性文字中的图像位置

时间:2019-01-14 17:43:05

标签: swift string uiimage nsattributedstring nstextattachment

我创建了带有一些文本和2张图像的属性文本。我想将其中一幅图像放在文本的前面,另一幅图像放在文本的末尾。到目前为止,效果很好。我的问题是我想将第二个图像的末尾像引号放在文本上方。我试图将该图像放置在顶部,而不是将其放置在底部。 / p>

这是我尝试过的:

let iconsSize = CGRect(x: 0, y: -5, width: 10, height: 10)
let attributedString = NSMutableAttributedString()
let attachment  = NSTextAttachment()
attachment.image = UIImage(named: "quote1")?.maskWithColor(color: #colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1))
attachment.bounds = iconsSize
attributedString.append(NSAttributedString(attachment: attachment))
if  let quotes = QuotesArray.instance.quotesArray.randomElement() {
    attributedString.append(NSAttributedString(string: "\n \(quotes) "))
}
let attachment2 = NSTextAttachment()
attachment2.image = UIImage(named: "quote2")?.maskWithColor(color: #colorLiteral(red: 0.7540688515, green: 0.7540867925, blue: 0.7540771365, alpha: 1))
attachment2.bounds = iconsSize
attributedString.append(NSAttributedString(attachment: attachment2))
quotesLbl.attributedText = attributedString

结果:

Result

0 个答案:

没有答案