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//赋值方法




你可能感兴趣的:(iOS)