解释我的情况的最佳示例是使用博客文章.假设我有一个 UITableView 加载了我从 API 获得的博客文章的标题.当我点击一行时,我想显示详细的博客文章.
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.
这样做时,API 会返回几个字段,包括post body"(即 HTML 文本).我的问题是,我应该用什么来显示它以便它显示为格式化的 HTML?我应该为此使用 UIWebView 吗?我不确定当您从字面上查看网页时是否使用 UIWebView(例如使用 URL 或其他内容对其进行初始化),或者您是否可以将其传递给 HTML 字符串并且它会正确格式化.
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.
还有其他几个字段将显示在此页面上,例如标题、类别、作者等.我只是使用 UILabels 来处理这些,所以那里没有问题.但我不知道如何处理 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.
如果你看不出来,我对 iOS 开发还比较陌生,只有大约 2-3 周的时间,没有 obj-c 背景.因此,如果 UIWebView 是正确的方法,我也会很感激任何陷阱!"笔记,如果有的话.
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.
正如David Liu所说,UIWebview 是要走的路.我会推荐一些单独构建 HTML 字符串,然后将其传递给 UIWebView.另外,我会使用 [webView setBackgroundColor:[UIColor clearColor]]
使背景透明,这样您就可以更轻松地让事情看起来像他们应该的样子.
As David Liu said, UIWebview is 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.
这是一个代码示例:
- (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"的好处是您可以通过整个解析操作继续构建字符串,然后将其传递给UIWebView",而NSString"一旦创建就无法更改.
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.
这篇关于如何在 iPhone 上显示来自 API 的 HTML 文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!