0

我收到来自 json 文件的图像、标题和描述,我需要在视图上显示它。我正在使用 webview,因为 description 属性有链接并且是 html 格式,所以 webview 是最简单的。但是现在我需要在描述上方添加图像和标题。我知道如何单独添加图像,但我不知道如何在 webview 中添加所有这三个组件。有什么帮助吗?谢谢

NSString * myHTMLImage = @"<img src='Hop.png'>";
NSString *imagePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:imagePath];
[self.webView loadHTMLString:myHTMLImage baseURL:baseURL];

上面的代码将图像嵌入到整个 webview 中。

[self.webView loadHTMLString:bodyOfText baseURL:nil]; 

我还需要能够执行 bodyOfText 的上述加载。还有一个标题。这是一个字符串。我该怎么做。

4

2 回答 2

1

您可以通过编程方式将这些添加为 UILabel 子视图,或者通过故事板通过拖出标签并将它们作为 IBOutlets 连接起来。

于 2012-06-28T15:41:40.857 回答
1

您只需构建您的 HTML 字符串。您可以执行以下操作:

NSString *title      = @"this is my title";
NSString *body       = [NSString stringWithFormat:@"Blah, blah, blah<p>%@<p>", self.bodyOfText.text];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *imgPath    = [bundlePath stringByAppendingPathComponent:@"Hop.png"];
NSURL    *imgUrl     = [NSURL fileURLWithPath:imgPath];

NSString *html = [NSString stringWithFormat:
                  @"<html>"
                  "<header>"
                  "<title>%@</title>"
                  "</header>"
                  "<body>"
                  "%@"
                  "<img src=\"%@\">"
                  "</body>"
                  "</html>",
                  title, body, [imgUrl absoluteString]];

[self.webView loadHTMLString:html baseURL:nil];

我通常在标签中构建对图像的引用,这样我就可以轻松地在同一个 HTML 字符串中src引用我的包中的图像以及我的文件夹中的图像。Documents

于 2012-06-28T15:49:18.527 回答