如何解决如何使用 NSMutableAttributedString 设置 UILabel不是 UITextView圆角背景?
无论如何我都需要在 UITableView 中创建代码样式。
简单制作蓝色背景
var dynamicBackground = #colorLiteral(red: 0.02708261088,green: 0.4768417478,blue: 0.9985074401,alpha: 1)
然后在 UITableView 中创建 NSMutableAttributedString
let attr = NSMutableAttributedString(string: "if ",attributes: [.foregroundColor: KeyboardSyntax])
attr.append(NSAttributedString(string: " false condition ",attributes: [.backgroundColor: dynamicBackground])). // Any idea to make background to round corner?
attr.append(NSAttributedString(string: " {\n ",attributes: [.foregroundColor: PlainSyntax]))
attr.append(NSAttributedString(string: "// Not execute\n",attributes: [.foregroundColor: CommentSyntax]))
attr.append(NSAttributedString(string: "} ",attributes: [.foregroundColor: PlainSyntax]))
attr.append(NSAttributedString(string: "else ",attributes: [.foregroundColor: KeyboardSyntax]))
attr.append(NSAttributedString(string: "{\n ",attributes: [.foregroundColor: PlainSyntax]))
attr.append(NSAttributedString(string: "// Execute\n",attributes: [.foregroundColor: CommentSyntax]))
attr.append(NSAttributedString(string: "} ",attributes: [.foregroundColor: PlainSyntax]))
cell.textLabel?.attributedText = attr
然后显示结果
感谢您的热心帮助。 :)
解决方法
我认为您必须自定义 NSLayoutManager,然后将属性添加到您构造的 NSAttributedString 中。 NSAttributedString background color and rounded corners 会有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。