Swift - lable 显示不同颜色字体

class MyLable: UILabel {

override func awakeFromNib() {

}

override func layoutSubviews() {

super.layoutSubviews()

let attributeStr = NSMutableAttributedString(string: self.text!)

attributeStr.addAttribute(NSForegroundColorAttributeName, value:UIColor.red , range:NSRange(location: 0,length: 2))

}

func changeTextColor(text: String, color: UIColor, range: NSRange) -> NSAttributedString {

let attributeStr = NSMutableAttributedString(string: text)

attributeStr.addAttribute(NSForegroundColorAttributeName, value:color , range: range)

return attributeStr

}

//

}

你可能感兴趣的:(Swift - lable 显示不同颜色字体)