如何在iPhone上显示来自API的HTML文本? [英] How to show HTML text from API on the iPhone?

查看:107
本文介绍了如何在iPhone上显示来自API的HTML文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解释我的情况的最好例子是使用博客文章。假设我有一个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.

此页面上还会显示其他几个字段,例如标题,类别,作者等。我只是使用UILabel,所以没有问题。但我不知道如何处理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文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆