码迷,mamicode.com
首页 > 编程语言 > 详细

Swift NSAttributedString的使用

时间:2018-05-30 15:09:41      阅读:2589      评论:0      收藏:0      [点我收藏+]

标签:stl   SM   gray   font   app   info   col   pac   span   

 

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的使用

标签:stl   SM   gray   font   app   info   col   pac   span   

原文地址:https://www.cnblogs.com/jukaiit/p/9110793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!