iOS swift- 同一控件显示不同颜色不同大小字体

2017-04-27  本文已影响248人  青菜白玉堂

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

}

(以下类比)

上一篇 下一篇

猜你喜欢

热点阅读