美文网首页
iOS swift- 同一控件显示不同颜色不同大小字体

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

作者: 青菜白玉堂 | 来源:发表于2017-04-27 18:53 被阅读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

}

(以下类比)

相关文章

网友评论

      本文标题:iOS swift- 同一控件显示不同颜色不同大小字体

      本文链接:https://www.haomeiwen.com/subject/ahsnzttx.html