2012-06-28 30 views
0

我从json文件接收图像,标题和描述,我需要在视图上显示它。我使用的是webview,因为description属性有链接并且是html格式,所以webview是最简单的。不过,现在我需要在说明上方添加图片和标题。我知道我可以如何分开添加图像,但我不知道如何在web视图中添加所有这三个组件。任何帮助?谢谢在UIWebView中嵌入图像和UILable

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

上面的代码嵌入到整个网页视图的图像。

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

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

回答

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]; 

我通常建立正确的src标签我对图像的引用,这样我可以很容易地引用我的包两个图像以及图像在我Documents文件夹中相同的HTML串。

1

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