How do you use NSAttributedString?

后端 未结 15 1128
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 04:19

Multiple colours in an NSString or NSMutableStrings are not possible. So I\'ve heard a little about the NSAttributedString which was introduced wit

15条回答
  •  不知归路
    2020-11-22 05:12

    In Swift 4:

    let string:NSMutableAttributedString = {
    
        let mutableString = NSMutableAttributedString(string: "firstsecondthird")
    
        mutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: NSRange(location: 0, length: 5))
        mutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.green , range: NSRange(location: 5, length: 6))
        mutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue , range: NSRange(location: 11, length: 5))
        return mutableString
    }()
    
    print(string)
    

提交回复
热议问题