ios 如何在 iPhone 上显示来自 API 的 HTML 文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4138336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show HTML text from API on the iPhone?
提问by rpheath
The best example to explain my situation is to use a blog post. Let's say I have a UITableView loaded with title's of blog posts that I got from an API. When I click on a row I want to show the detailed blog post.
解释我的情况的最佳示例是使用博客文章。比方说,我有一个UITableView满载博客文章标题的,我从一个API了。当我单击一行时,我想显示详细的博客文章。
When doing that, the API is handing back several fields, including the "post body" (which is HTML text). My question is, what should I use to display it so it shows up as formatted HTML? Should I use a UIWebView for that? I'm not sure if you use a UIWebView when you are literally viewing a web page (like initialize it with a URL or something) or if you can hand it an HTML string and it will format it properly.
这样做时,API 会交回几个字段,包括“帖子正文”(即 HTML 文本)。我的问题是,我应该用什么来显示它以便它显示为格式化的 HTML?我应该为此使用 UIWebView 吗?我不确定当您真正查看网页时是否使用 UIWebView(例如用 URL 或其他东西初始化它),或者您是否可以将它传递给一个 HTML 字符串并且它会正确地格式化它。
There are several other fields that will be showing on this page, such as title, category, author, etc. I'm just using UILabels for those, so no problems there. But I don't know what to do with the HTML chunk. I'm doing all of this programmatically, btw.
还有其他几个字段将显示在此页面上,例如标题、类别、作者等。我只是为这些使用 UILabels,所以没有问题。但我不知道如何处理 HTML 块。我正在以编程方式完成所有这些,顺便说一句。
If you can't tell, I'm relatively new to iOS development, only about 2-3 weeks in, with NO obj-c background. So if a UIWebView is the right approach, I'd also appreciate any "gotcha!" notes, if there are any.
如果你不知道,我对 iOS 开发比较陌生,只有大约 2-3 周,没有 obj-c 背景。因此,如果 UIWebView 是正确的方法,我也会感谢任何“问题”!笔记,如果有的话。
回答by Moshe
As David Liu said, UIWebviewis the way to go. I would recommend a few building the HTML string separately and then passing it to the UIWebView. Also, I'd make the background transparent, using [webView setBackgroundColor:[UIColor clearColor]]
so that you have an easier time making things look as they should.
正如 David Liu 所说,UIWebview是要走的路。我会推荐一些单独构建 HTML 字符串,然后将其传递给 UIWebView。另外,我会让背景透明,使用[webView setBackgroundColor:[UIColor clearColor]]
这样你就可以更轻松地让事情看起来像他们应该的那样。
Here's a code sample:
这是一个代码示例:
- (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 benefit to using a 'NSMutableString' is that you can continue to build your string through an entire parsing operation and then pass it to the the 'UIWebView', whereas a 'NSString' cannot be changed once it is created.
使用“NSMutableString”的好处是您可以通过整个解析操作继续构建字符串,然后将其传递给“UIWebView”,而“NSString”一旦创建就无法更改。
回答by Pedroinpeace
self.textLbl.attributedText = [[NSAttributedString alloc] initWithData: [@"html-string" dataUsingEncoding:NSUnicodeStringEncoding]
options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
} documentAttributes:nil error:nil];
回答by neha
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'm using this code to even set the font for the webview text and passing my ivar 'ParameterWhereYouStoreTextFromAPI' where I'm storing text obtained from api.
我正在使用此代码甚至设置 webview 文本的字体并传递我的 ivar 'ParameterWhereYouStoreTextFromAPI',我将在其中存储从 api 获得的文本。
回答by Ortwin Gentz
In the special case of primitive HTML (text styles, p/br tags) you can also use UITextView
with an undocumented property:
在原始 HTML(文本样式、p/br 标签)的特殊情况下,您还可以使用UITextView
未记录的属性:
-[UITextView setValue:@"<b>bold</b>" forKey:@"contentToHTMLString"]
Even though it's undocumented, it's used in many apps I know of and so far hasn't caused a single rejection.
尽管它没有记录在案,但它已用于我所知道的许多应用程序中,到目前为止还没有引起任何拒绝。
回答by David Liu
回答by kraag22
Moshe's answer is great, but if you want transparent webview, you need to set property opaque to FALSE.
Moshe 的回答很好,但是如果你想要透明的 webview,你需要将属性 opaque 设置为 FALSE。
You may like to check : How to make a transparent UIWebVIew
您可能想检查:如何制作透明的 UIWebVIew
回答by Anuj Kumar Rai
You can show it by removing HTML/JS text like (align,centre,br>).Please use this methods if you are targeting iOS7.0 and above.
您可以通过删除 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];
回答by Mili
My scenario: I have a Textview in a view controller and have to show data in the textView which is in HTML format.
我的场景:我在视图控制器中有一个 Textview,并且必须在 HTML 格式的 textView 中显示数据。
Swift 3:
斯威夫特 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.
:)