1. 程式人生 > >swift3 UILabel部分字型變顏色,修改行間距

swift3 UILabel部分字型變顏色,修改行間距

需要注意:

make.left.equalToSuperview().offset(15)

make.right.equalToSuperview().offset(-15)//不能少的一句

upLeftLabel.snp.makeConstraints { (make) in

            make.top.equalTo(cancelBtn.snp.bottom).offset(30)

            make.left.equalToSuperview().offset(15)

            make.right.equalToSuperview().offset(-15)//

        }


//【精選預測】變為紅色

let strg ="【精選預測】廣州恆大爭三分,上海申花衝擊三連勝"

        let ranStr = "【精選預測】"

//所有文字變為富文字

let attrstring:NSMutableAttributedString =NSMutableAttributedString(string:strg)

//顏色處理的範圍

        let str = NSString(string: strg)

        let theRange = str.range(of: ranStr)

        //顏色處理

        attrstring.addAttribute(NSAttributedStringKey

.foregroundColor, value:UIColor.red, range: theRange)

        //行間距

        let paragraphStye = NSMutableParagraphStyle()

        paragraphStye.lineSpacing = 5

        //行間距的範圍

        let distanceRange = NSMakeRange(0, CFStringGetLength(strg as CFString!))

        attrstring .addAttribute(NSAttributedStringKey

.paragraphStyle, value: paragraphStye, range: distanceRange)

        upLeftLabel.attributedText = attrstring//賦值方法