如何从iPhone上的API显示HTML文本?

解释我的情况的最好的例子是使用博客文章。 比方说,我有一个UITableView加载了我从API获取的博客文章的标题。 当我点击一行,我想显示详细的博客文章。

当这样做的时候,API会传回几个字段,包括“post body”(这是HTML文本)。 我的问题是,我应该使用什么来显示它,所以它显示为格式的HTML? 我应该使用一个UIWebView的? 我不确定当你真的在查看一个网页时是否使用了一个UIWebView(比如用一个URL或者其他东西来初始化它),或者如果你能把它传给一个HTMLstring,它将会正确地格式化它。

在这个页面上还会显示其他几个字段,例如标题,类别,作者等。我只是使用UILabels,所以没有问题。 但我不知道如何处理HTML块。 我正在做所有这个程序,顺便说一句。

如果你不能说,我是相对较新的iOS开发,只有约2-3周,没有obj-c背景。 所以如果一个UIWebView是正确的方法,我也很感激任何“困扰!” 笔记,如果有的话。

正如大卫刘说, UIWebview是要走的路。 我会build议一些单独的HTMLstring,然后传递给UIWebView。 此外,我会使背景透明,使用[webView setBackgroundColor:[UIColor clearColor]]让你有一个更容易的时间看起来他们应该看起来。

这是一个代码示例:

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

注意:

使用'NSMutableString'的好处是你可以继续通过一个完整的parsing操作来构build你的string,然后把它传递给'UIWebView',而'NSString'一旦创build就不能改变。

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

我使用这个代码甚至设置webview文本的字体,并传递我的ivar“ParameterWhereYouStoreTextFromAPI”,用于存储从api获取的文本。

  self.textLbl.attributedText = [[NSAttributedString alloc] initWithData: [@"html-string" dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; 

在原始HTML(文本样式,p / br标签)的特殊情况下,您还可以使用带有未logging属性的UITextView

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

即使它没有logging,但它被用在我所知道的许多应用程序中,至今还没有引起任何拒绝。

你可以使用UIWebView的 – loadHTMLString:baseURL:方法。

参考链接: 这里

Moshe的答案很好,但如果你想透明的webview,你需要设置属性不透明为FALSE。

你可能想要检查: 如何制作一个透明的UIWebVIew

您可以通过删除HTML / JS文本(如align,center,br>)来显示它。如果您的目标是iOS7.0或更高版本,请使用此方法。

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

我的场景:我在视图控制器中有一个Textview,并且必须在HTML格式的textView中显示数据。

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. :)