@IBOutlet weak var demoLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//定义两个字符串
let attrStr = NSAttributedString(string: "李亚飞", attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
let attrStr1 = NSAttributedString(string: "很不错", attributes: [NSForegroundColorAttributeName : UIColor.blueColor()])
//图文混排
let attacment = NSTextAttachment()
attacment.image = UIImage(named: "d_aini")
let font = demoLabel.font
attacment.bounds = CGRectMake(0, -4, font.lineHeight, font.lineHeight)
let attrImageStr = NSAttributedString(attachment: attacment)
let attrMStr = NSMutableAttributedString()
attrMStr.appendAttributedString(attrStr)
attrMStr.appendAttributedString(attrImageStr)
attrMStr.appendAttributedString(attrStr1)
demoLabel.attributedText = attrMStr
}
显示结果
网友评论