swift - 属性文本中的图像位置
问题描述
我创建了一个带有一些文本和 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
结果:
解决方案
推荐阅读
- angular - 拆分具有太多属性的类 - 角度
- python - 运行 CMAKE 找不到 python 可执行文件
- swiftui - 在没有导航链接的情况下更改视图 SwiftUI
- python - 如何从 Mac 上的另一个可执行文件/Python 脚本调用可执行文件?
- python-3.x - Heroku Python没有名为Mysql的模块
- android-studio - 如何在 android studio 中为 artifactory 创建 pom 文件?
- javascript - 我怎样才能等待 2 个承诺完成,然后再运行另一个?
- django - django中的slug错误-模板显示每个帖子中的所有帖子数据
- kubernetes - 使用外部 Prometheus 抓取 CoreDNS 指标
- c++ - 索引访问器时的 EXC_BAD_ACCESS (code=EXC_I386_GPFLT)