在UIWebView中嵌入Image和UILable [英] Embedding Image and UILable in UIWebView

查看:93
本文介绍了在UIWebView中嵌入Image和UILable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am receiving an image, title and description from a json file and i need to display this on a view. I am using webview because the description attribute has links and is in html format so webview is easiest. However now i need to add the image and title above the description. I know how i can add an image seperately but i dont know how i can add all these three components in the webview. Any help? Thank you

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

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

The above code embeds an image to the entire webview.

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

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

I need to be able to do the above loading of bodyOfText as wel. and a title too. which is a string. How do i do it.

推荐答案

您只需构建HTML字符串即可。你可以这样做:

You just build up your HTML string. You could do something like:

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 标签,这样我就可以轻松地引用我的包中的两个图像以及同一HTML字符串中 Documents 文件夹中的图像。

I usually build my references to images right in the src tag, that way I can easily refer to both images in my bundle as well as images in my Documents folder in the same HTML string.

这篇关于在UIWebView中嵌入Image和UILable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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