您的位置:首页 > 移动开发 > Swift

Swift笔记 label 显示富文本或HTML

2016-04-03 12:13 561 查看
Label中显示自定义颜色或者大小的方式如下

自定义

//富文本设置
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))
//设置字体颜色
attributeString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(),
range: NSMakeRange(0, 3))
//设置文字背景颜色
attributeString.addAttribute(NSBackgroundColorAttributeName, value: UIColor.greenColor(),
range: NSMakeRange(3,3))

msgLabel.attributedText = attributeString


显示 html

var htmlText = "空は<font color=\"blue\">青い</font>。<br>An apple is <font color=\"red\">red</font>."
do{
let attrStr = try NSAttributedString(data: htmlText.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

msgLabel.attributedText = attrStr
}catch let error as NSError {
print(error.localizedDescription)
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  标签 swift html label