How to show HTML text from API on iPhone?

The best example to explain my situation is to use a blog post. Say I have a UITableView loaded with the blog post headers I received from the API. When I click on the line, I want to show a detailed blog post.

In this case, the API passes several fields, including the "message body" (this is the HTML text). My question is: what should I use to display it, so that it displays as formatted HTML? Should I use UIWebView for this? I'm not sure if you use UIWebView when you literally browse a web page (for example, initialize it with a URL or something), or if you can pass an HTML string to it and it will format it correctly.

This page will display several other fields, such as title, category, author, etc. I just use UILabels for them, so there is no problem. But I do not know what to do with a piece of HTML. I do all this programmatically, by the way.

If you can’t say, I'm relatively new to iOS development, only about 2-3 weeks, with NO obj-c background. Therefore, if UIWebView is the right approach, I would also appreciate any “received”! notes, if any.

+42
ios objective-c iphone uitextview uiwebview
Nov 09 '10 at 20:36
source share
8 answers

As David Liu said, UIWebview is the way to go. I would recommend building the HTML string separately and then passing it to a UIWebView. In addition, I would make the background transparent using [webView setBackgroundColor:[UIColor clearColor]] so that it is easier for you to do everything as it should.

Here is a sample code:

 - (void) createWebViewWithHTML{ //create the string NSMutableString *html = [NSMutableString stringWithString: @"<html><head><title></title></head><body style=\"background:transparent;\">"]; //continue building the string [html appendString:@"body content here"]; [html appendString:@"</body></html>"]; //instantiate the web view UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame]; //make the background transparent [webView setBackgroundColor:[UIColor clearColor]]; //pass the string to the webview [webView loadHTMLString:[html description] baseURL:nil]; //add it to the subview [self.view addSubview:webView]; } 

Note:

The advantage of using "NSMutableString" is that you can continue to build your string with the entire parsing operation and then pass it to "UIWebView", while "NSString" cannot be changed after it is created.

+52
Nov 09 2018-10-11T00:
source share
 NSString *strForWebView = [NSString stringWithFormat:@"<html> \n" "<head> \n" "<style type=\"text/css\"> \n" "body {font-family: \"%@\"; font-size: %@; height: auto; }\n" "</style> \n" "</head> \n" "<body>%@</body> \n" "</html>", @"helvetica", [NSNumber numberWithInt:12], ParameterWhereYouStoreTextFromAPI]; [self.webview loadHTMLString:strForWebView baseURL:nil]; 

I use this code to even set the font for webview text and pass my ivar 'ParameterWhereYouStoreTextFromAPI', where I store the text obtained from the api.

+7
Nov 10 '10 at 7:54
source share
  self.textLbl.attributedText = [[NSAttributedString alloc] initWithData: [@"html-string" dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 
+7
Apr 09 '14 at 8:01
source share

In the special case of primitive HTML (text styles, p / br tags), you can also use a UITextView with an undocumented property:

 -[UITextView setValue:@"<b>bold</b>" forKey:@"contentToHTMLString"] 

Despite the fact that it is not documented, it is used in many applications that I know and still have not caused a single deviation.

+6
Sep 14 '12 at 19:11
source share

You can use the UIWebView - loadHTMLString: baseURL: method.

Link Link: here

+4
Nov 09 '10 at 20:42 on
source share

Moshe's answer is great, but if you want transparent web browsing, you need to set the opaque property to FALSE.

You can check: How to make transparent UIWebVIew

+2
Sep 10 '12 at 14:40
source share

You can show this by removing the HTML / JS text, e.g. (align, center, br>). Use these methods if you are targeting iOS7.0 or higher.

  NSString *htmlFile; htmlFile=[array valueForKey:@"results"]; NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[htmlFile dataUsingEncoding:NSUTF8StringEncoding]options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}documentAttributes:nil error:nil]; NSLog(@"html: %@", htmlFile); NSLog(@"attr: %@", attr); NSLog(@"string: %@", [attr string]); NSString *finalString = [attr string]; [webView loadHTMLString:[finalString description] baseURL:nil]; 
0
Feb 01 '16 at 16:23
source share

My scenario: I have a Textview in the view controller and should display data in a textView, which is in HTML format.

Swift 3:

 func detectUrlInText() { let attrStr = try! NSAttributedString( data: "<b><i>\(Ldesc)</i></b>".data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) desc.attributedText = attrStr desc.font = UIFont(name: "CALIBRI", size: 14) } // Ldesc is the string which gives me the data to put in the textview. desc is my UITextView. :) 
0
Nov 28 '16 at 9:50
source share



All Articles