当前位置: 移动技术网 > 移动技术>移动开发>IOS > Swift NSAttributedString的使用

Swift NSAttributedString的使用

2018年05月30日  | 移动技术网移动技术  | 我要评论
NSMutableAttributedString let testAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.backgroundColor: UIColor.ye ...

 

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网