swift 指定文字(包括下划线)特殊颜色

override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.black
        let label = UILabel.init(frame: CGRect(x:10, y: 30, width:400 , height:30 ))
        label.backgroundColor = UIColor.white
        self.view .addSubview(label)
        let sub = "如果您是VIP成员,请联系客服重置密码"
        label.attributedText = self.getNSAttributedString(str: sub)
    }
    //指定字符 指定颜色并加上下划线
    func getNSAttributedString(str:String) -> NSAttributedString {
        let myMutableString = NSMutableAttributedString(string: str)
        let range2 = NSMakeRange(12, 4)
        myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.green, range: range2)
        myMutableString.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleNone.rawValue, range: NSMakeRange(0, 12))
        myMutableString.addAttribute(NSUnderlineStyleAttributeName , value: NSUnderlineStyle.styleSingle.rawValue, range: range2)
        return myMutableString
    }

效果图:

swift 指定文字(包括下划线)特殊颜色_第1张图片
屏幕快照 2017-09-12 11.06.13.png

你可能感兴趣的:(swift 指定文字(包括下划线)特殊颜色)