Namhcir
Namhcir

Reputation: 815

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 displays, 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.

Upvotes: 1

Views: 1174

Answers (3)

ndraniko
ndraniko

Reputation: 824

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;

Upvotes: 0

Ali Awais
Ali Awais

Reputation: 113

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];

Upvotes: 2

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181460

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.

Upvotes: 2

Related Questions