Swift中对UILable的文字大小、颜色、行间距、富文本属性等设置
goodsLab.text = "潜水艇防臭淋雨房地漏套餐地漏3+直接头1+洗衣机龙头*1LTK50—/10X/L702X"
goodsLab.textColor = UIColor.convertHexColorToUIColor("666666")
goodsLab.font = UIFont.systemFontOfSize(30.px)
//设置行间距
let attributedString = NSMutableAttributedString(string:goodsLab.text!)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 15.px
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, NSString(string: goodsLab.text!).length))
goodsLab.attributedText = attributedString
goodsLab.adjustsFontSizeToFitWidth = true
goodsLab.numberOfLines=0
goodsLab.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(goodsLab)
//富文本设置
var attributeString = NSMutableAttributedString(string:"welcome to hangge.com")
//从文本0开始6个字符字体HelveticaNeue-Bold,16号
attributeString.addAttribute(NSFontAttributeName, value: UIFont(name: "HelveticaNeue-Bold", size: 16)!,range: NSMakeRange(0,6))
//设置字体颜色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),range: NSMakeRange(0, 3))
//设置文字背景颜色
attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(),range: NSMakeRange(3,3))
msgLabel.attributedText = attributeString
let label1=UILabel(frame: CGRectMake(30, 120, 300, 36))
label1.text="测试Swift UILabel 这里测试一下文字大小与标签宽度自适应 和设置最小缩放比例"
self.view .addSubview(label1)
label1.backgroundColor=UIColor.grayColor()
let label2=UILabel(frame: CGRectMake(30, 170, 300, 36))
label2.text="测试Swift UILabel 这里测试一下文字大小与标签宽度自适应"
label2.adjustsFontSizeToFitWidth=true
self.view .addSubview(label2)
label2.backgroundColor=UIColor.grayColor()
let label3=UILabel(frame: CGRectMake(30, 220, 300, 36))
label3.text="测试Swift UILabel 这里测试一下文字大小与标签宽度自适应"
label3.adjustsFontSizeToFitWidth=true
//此处的0.6是缩小到0.6倍就不缩小了,如果还是超过那么就省略显示多余部分
label3.minimumScaleFactor=0.6
self.view .addSubview(label3)
label3.backgroundColor=UIColor.grayColor()