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.