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
}
效果图: