【问题标题】:Embedding Image and UILable in UIWebView在 UIWebView 中嵌入图像和 UILable
【发布时间】:2012-06-28 15:20:20
【问题描述】:

我收到来自 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 加载。还有一个标题。这是一个字符串。我该怎么做。

【问题讨论】:

    标签: ios iphone objective-c ios5 uiwebview


    【解决方案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 标记中构建对图像的引用,这样我可以轻松地在同一 HTML 字符串中引用我的包中的图像以及 Documents 文件夹中的图像。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2011-04-18
        • 1970-01-01
        • 2013-08-28
        • 2016-06-09
        • 2012-01-20
        • 1970-01-01
        • 2018-05-14
        • 1970-01-01
        • 2015-01-01
        相关资源
        最近更新 更多