首页 > 解决方案 > 属性文本中的图像位置

问题描述

我创建了一个带有一些文本和 2 个图像的属性文本。我想将其中一张图片放在前面,另一张放在文字后面。到目前为止效果很好。我的问题是我希望最后的第二张图片像引号"一样放在文本上方。我试图将该图片放在顶部,而不是放在底部。

这是我尝试过的:

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

结果:

结果

标签: swiftstringuiimagensattributedstringnstextattachment

解决方案


推荐阅读