let lableText = UILabel(frame: CGRect(x: 10, y: 200, width: 100, height: 30))
lableText.text = "消费:100 元"
let attributedString = NSMutableAttributedString(string: lableText.text!)
attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.red, range: NSMakeRange(3, ( lableText.text?.length())!-4))
attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 16), range: NSMakeRange(3, ( lableText.text?.length())!-4))
lableText.attributedText = attributedString
view.addSubview(lableText)
//lable控件,字体大小,字体颜色,开始位置,设置长度
func LableTextColor(lab:UILabel,sizeFont:CGFloat,textColor: Any,startInt:Int,len:Int) -> () {
let attributedString = NSMutableAttributedString(string: lab.text!)
attributedString.addAttribute(NSForegroundColorAttributeName, value: textColor, range: NSMakeRange(startInt, len))
attributedString.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: sizeFont), range: NSMakeRange(startInt, len))
lab.attributedText = attributedString
}
(以下类比)