let str = "fdasfsafaf"
letnameStr:NSMutableAttributedString=NSMutableAttributedString(string: str)
//绿色
letrange =NSMakeRange(0,5)
nameStr.addAttribute(NSForegroundColorAttributeName,value:UIColor.greenColor(),range: range)
//白色
let range2 = NSMakeRange(5,str.characters.count-5)
let lbl = UILabel(frame:
CGRectMake(0,0,100,40))
lbl.attributedText= nameStr
原文链接:https://www.f2er.com/swift/325589.html