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

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

}


(以下类比)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容