iOS富文本
最简单的方法是使用UIWebView.因为这家伙直接就支持html代码.
看手册:
loadHTMLString:baseURL:
Sets the main page content and base URL.
Parameters
- string
- The content for the main page.
- baseURL
- The base URL for the content.
Availability
- Available in iOS 2.0 and later.
Declared In
UIWebView.h
[webView loadHTMLString:@”<ul><li>aaa</li><li>abbbb</li><li style=’text-decoration:underline;’>cccc</li></ul>” baseURL:nil];
看下效果:
有点需要注意,使用自己的html串时
scalesPageToFit要设为NO(默认值).
看来起UIWebView是不错,功能强大.但他也有一些需要注意的地方:
Important You should not embed UIWebView
or UITableView
objects in UIScrollView
objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
为了避免事件混淆,别把UIWebView嵌到UIScrollView里去,要不然会有一些草名其妙的问题的.
还有就是下面的缺点了:
1. UIWebView效率太低,不适合嵌套到列表UITableView里
2. UIWebView只能在加载完后才能通过Javascript查到内容高度
3. UIWebView封装太过,不可以被继承,屏蔽了用户触摸事件,很难对View进行自定义化,只能通过调整HTML代码来修改样式。
除了UIWebView,还可以参看其它几种方式:
1.Facebook Three20的TTStyledText,
2.苹果iOS3.2新加库CoreText的NSAtrributedString
3.Zynga FontLabel的ZAttributedString.