ios-如何在iPhone上显示来自API的HTML文本?
解释我的情况的最佳示例是使用博客文章。 假设我有一个UITableView,其中装有我从API获得的博客文章的标题。 当我单击一行时,我想显示详细的博客文章。
这样做时,API会交出几个字段,包括“帖子正文”(HTML文本)。 我的问题是,我应该用什么来显示它,使其显示为格式化的HTML? 我应该为此使用UIWebView吗? 我不确定在实际浏览网页时是否使用UIWebView(例如使用URL或其他内容初始化网页),或者可以将其传递给HTML字符串并将其正确格式化。
此页面上还将显示其他几个字段,例如标题,类别,作者等。我仅使用UILabel来实现这些功能,因此那里没有问题。 但是我不知道该如何处理HTML块。 我正在以编程方式完成所有这些,顺便说一句。
如果您不能确定,我是iOS开发的新手,大约只有2-3周,没有obj-c背景。 因此,如果UIWebView是正确的方法,我也将不胜感激! 注意,如果有的话。
8个解决方案
54 votes
正如David Liu所说,UIWebview是必经之路。 我建议一些单独构建HTML字符串,然后将其传递给UIWebView的方法。 另外,我将使用[webView setBackgroundColor:[UIColor clearColor]]使背景透明,这样您就可以更轻松地使外观看起来应有的样子。
这是一个代码示例:
- (void) createWebViewWithHTML{
//create the string
NSMutableString *html = [NSMutableString stringWithString: @"
"];//continue building the string
[html appendString:@"body content here"];
[html appendString:@""];
//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”的好处是,您可以继续通过整个解析操作来构建字符串,然后将其传递给“ UIWebView”,而一旦创建了“ NSString”就无法对其进行更改。
Moshe answered 2020-06-24T19:44:35Z
9 votes
self.textLbl.attributedText = [[NSAttributedString alloc] initWithData: [@"html-string" dataUsingEncoding:NSUnicodeStringEncoding]
options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
} documentAttributes:nil error:nil];
Pedroinpeace answered 2020-06-24T19:44:51Z
7 votes
NSString *strForWebView = [NSString stringWithFormat:@" \n"
"
\n""
"body {font-family: \"%@\"; font-size: %@; height: auto; }\n"
" \n"
" \n"
"
%@ \n""", @"helvetica", [NSNumber numberWithInt:12], ParameterWhereYouStoreTextFromAPI];
[self.webview loadHTMLString:strForWebView baseURL:nil];
我正在使用此代码甚至为Webview文本设置字体,并传递我的ivar'ParameterWhereYouStoreTextFromAPI'来存储从api获得的文本。
neha answered 2020-06-24T19:45:11Z
6 votes
在原始HTML的特殊情况下(文本样式,p / br标签),您还可以使用具有未记录属性的UITextView:
-[UITextView setValue:@"bold" forKey:@"contentToHTMLString"]
即使它没有记录,但它在我所知道的许多应用程序中都使用过,到目前为止,还没有引起任何拒绝。
Ortwin Gentz answered 2020-06-24T19:45:36Z
4 votes
您可以使用UIWebView的-loadHTMLString:baseURL:方法。
参考链接:此处
David Liu answered 2020-06-24T19:46:00Z
2 votes
Moshe的回答很好,但是如果您想要透明的Web视图,则需要将opaque属性设置为FALSE。
您可能想检查一下:如何制作透明的UIWebVIew
kraag22 answered 2020-06-24T19:46:24Z
0 votes
您可以通过删除(align,centre,br>)之类的HTML / JS文本来显示它。如果您定位的是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];
Anuj Kumar Rai answered 2020-06-24T19:46:44Z
0 votes
我的场景:我在视图控制器中有一个Textview,并且必须在textView中以HTML格式显示数据。
斯威夫特3:
func detectUrlInText() {
let attrStr = try! NSAttributedString(
data: "\(Ldesc)".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.
:)
Mili answered 2020-06-24T19:47:08Z