NSString render HTML tags?
I am developing an app that displays content that is also displayed on a web page version of the application. I am given the same marked-up text that the web page dis开发者_JS百科plays, but in a UITextView.
The text has HTML tags embedded, such as <BR>
, <p>
, and <em>
for break, paragraph, and bold (emphasis), respectively.
Is there an encoding for NSString or for text in a UITextView that can render HTML tags as they would display in a web page? I want to see the break, new paragraph, bold, etc, etc, not just to strip off the html tags. Thanks in advance.
You can use a UIWebView to view HTML CONTENT on your iPhone Application instead of using UITextView
. NSString
stores as string. It doesn't render it.
Following is simple code to load html from string in UIWebView. You can customize your webview using its other properties, for user interaction on links in html you need to implement UIWebView delegates.
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 0, 300, 300)];
NSString *html = [NSString stringWithFormat:@"%@", YOUR_HTML_GOES_HERE];
[webView loadHTMLString:html baseURL:[NSURL URLWithString:@""]];
[self addSubview:webView];
[webView release];
You can use UITextView to render html using NSAttributedString as follows (available on iOS 7.0 and later):
NSAttributedString *attributedString = [[NSAttributedString alloc]
initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding]
options:@{
NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType
}
documentAttributes:nil
error:nil];
textView.attributedText = attributedString;
精彩评论