1

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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you help with a sample? From what I read, you load a UIWebView from a URL. I do't have a url to load from, rather only html-marked-up text (NSString) in a plist. I don't see a myWebView.text (or myWebView.backgroundColor, for that matter) method like for a UITextView. Thanks!
2

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

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.