A UITextView is great for displaying multiple lines of simple text. But when you need something more fancy, like headlines or highlighting text, then you should look at UIWebView. It’s not just capable of displaying web pages from a URL, but you can also specify the HTML you want to display as a string.
Create a UIWebView in a controller:
- - (void)loadView
- {
- // Create a custom view hierarchy.
- CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
- UIView *view = [[UIView alloc] initWithFrame:appFrame];
- view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
- self.view = view;
- [view release];
- CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
- webView = [[UIWebView alloc] initWithFrame:webFrame];
- webView.backgroundColor = [UIColor whiteColor];
- [self.view addSubview:webView];
- }
Then you can add your rich text to the view like this:
- NSString *html = @"<html><head><title>The Meaning of Life</title></head><body><p>...really is <b>42</b>!</p></body></html>";
- [webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.hitchhiker.com/message"]];
The documentation is not very clear on what the baseURL is used for in the context of HTML that is loaded from a string.
本文介绍如何利用UIWebView在iOS应用中展示包含标题、加粗等格式的富文本内容。通过创建并配置UIWebView,可以加载自定义的HTML字符串来实现复杂的文本样式。

被折叠的 条评论
为什么被折叠?



