Swift中對UILable的文字大小衅金、顏色、行間距簿煌、富文本屬性等設(shè)置
goodsLab.text = "潛水艇防臭淋雨房地漏套餐地漏3+直接頭1+洗衣機龍頭*1LTK50—/10X/L702X"
goodsLab.textColor = UIColor.convertHexColorToUIColor("666666")
goodsLab.font = UIFont.systemFontOfSize(30.px)
//設(shè)置行間距
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)
//富文本設(shè)置
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))
//設(shè)置字體顏色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),range: NSMakeRange(0, 3))
//設(shè)置文字背景顏色
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 這里測試一下文字大小與標(biāo)簽寬度自適應(yīng) 和設(shè)置最小縮放比例"
self.view .addSubview(label1)
label1.backgroundColor=UIColor.grayColor()
let label2=UILabel(frame: CGRectMake(30, 170, 300, 36))
label2.text="測試Swift UILabel 這里測試一下文字大小與標(biāo)簽寬度自適應(yīng)"
label2.adjustsFontSizeToFitWidth=true
self.view .addSubview(label2)
label2.backgroundColor=UIColor.grayColor()
let label3=UILabel(frame: CGRectMake(30, 220, 300, 36))
label3.text="測試Swift UILabel 這里測試一下文字大小與標(biāo)簽寬度自適應(yīng)"
label3.adjustsFontSizeToFitWidth=true
//此處的0.6是縮小到0.6倍就不縮小了氮唯,如果還是超過那么就省略顯示多余部分
label3.minimumScaleFactor=0.6
self.view .addSubview(label3)
label3.backgroundColor=UIColor.grayColor()