Swift NSAttributedString的使用
Posted 鸿鹄当高远
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift NSAttributedString的使用相关的知识,希望对你有一定的参考价值。
NSMutableAttributedString
let testAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.backgroundColor: UIColor.yellow,NSAttributedStringKey.strikethroughStyle:1] as [NSAttributedStringKey : Any]
let testAttributeString = NSAttributedString(string: "富文本测试", attributes:testAttributes)
self.testLabel.attributedText = testAttributeString
let prefix = "¥" + "100"
let suffix = "¥" + "200"
let string = prefix + suffix
let myAttribute = [NSAttributedStringKey.foregroundColor: UIColor.lightGray,
NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 13)] as [NSAttributedStringKey : Any]
let redAttribute = [NSAttributedStringKey.foregroundColor: UIColor.red,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)] as [NSAttributedStringKey : Any]
let attString = NSMutableAttributedString(string: string)
attString.addAttributes(redAttribute, range:NSRange.init(location: 0, length: prefix.count))
attString.addAttributes(myAttribute, range: NSRange.init(location: prefix.count, length: suffix.count))
self.titleLabel.attributedText = attString
以上是关于Swift NSAttributedString的使用的主要内容,如果未能解决你的问题,请参考以下文章
Swift - 带有链接的 HTML 风格化 NSAttributedString
显示带有不间断空格和多行的 NSAttributedString 的 Swift 错误