使用步骤
NSMutableAttributedString初始化
引用方法
给label等赋值(attributedText)
封装方法
//传入字符串、字体 返回NSMutableAttributedString
private func appendStrWithString(str:String,font:CGFloat) -> NSMutableAttributedString {
var attributedString : NSMutableAttributedString
let attStr = NSMutableAttributedString.init(string: str, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: font)])
attributedString = NSMutableAttributedString.init(attributedString: attStr)
return attributedString
}
//传入字符串、字体、颜色 返回NSMutableAttributedString
private func appendColorStrWithString(str:String,font:CGFloat,color:UIColor) -> NSMutableAttributedString {
var attributedString : NSMutableAttributedString
let attStr = NSMutableAttributedString.init(string: str, attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: font),NSForegroundColorAttributeName:color])
attributedString = NSMutableAttributedString.init(attributedString: attStr)
return attributedString
}
引用方法
let attributedString = NSMutableAttributedString.init()//初始化 let needIntegral = json["NeedIntegral"].intValue attributedString.appendAttributedString(self.appendColorStrWithString("您还需要", font: 14, color: UIColor.init(102, 102, 102)))attributedString.appendAttributedString(self.appendColorStrWithString("\(needIntegral)", font: 14, color: UIColor.init(223, 51, 72))) attributedString.appendAttributedString(self.appendColorStrWithString("积分才能兑换该商品", font: 14, color: UIColor.init(102, 102, 102))) self.alertContentLabel.attributedText = attributedString
转载 http://blog.csdn.net/Soar2/article/details/52777577?locationNum=7