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) }
相关文章推荐
- 在Windows 8.1的IE 11中屏蔽双击放大功能
- Apple Swift学习教程
- Apple官网研究之使用Justify布局导航
- 介绍 Fedora 上的 Swift
- 通过Mootools 1.2来操纵HTML DOM元素
- 表格标签table深入了解
- jQuery Html控件基本操作(日常收集整理)
- WEB标准网页布局中尽量不要使用的HTML标签
- Flash 与 html 的一些实用技巧
- 如何使用jquery easyui创建标签组件
- html工作中表格<tbody>标签的使用技巧
- HTML 向 XHTML1.0 兼容性指导
- C#自写的一个HTML解析类(类似XElement语法)
- 没有文件大小限制并免费的PDF到HTML转换工具
- JavaScript与HTML结合的基本使用方法整理
- css实现气泡框效果(实例加图解)
- html链接与文本标签们