以编程方式创建WebView并在视图中显示NSStrings [英] Creating a WebView programmatically and displaying NSStrings inside the view

查看:77
本文介绍了以编程方式创建WebView并在视图中显示NSStrings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式创建了一个WebView。这很完美。代码如下。
我现在需要尝试做的是将 NSStrings 注入其中。我有一个30个字符串的数组。 15个标题和15个正文。

I have created a WebView programmatically. This works perfect. The code is below. What I need to try and do now is inject NSStrings into it. I have an array of 30 strings. 15 Headings and 15 body-texts.

是否可以在WebView中显示这些内容?我猜我需要将它们改成HTML,我可能会将它们全部重新格式化为1个长 NSString ,其中包含HTML标签和换行符以及newling-tags可能吗?

Is it possible to get these displayed inside the WebView? I'm guessing I need to change them into a HTML, i may be reformat them all into 1 long NSString with HTML-tags and linebreaks and newling-tags maybe?

任何人都可以帮我一些指针/代码片段让我走上正轨并转向正确的方向吗?

Anybody able to help me with some pointers/code snippets to get me on the right track and moving the the correct direction please?

- (void) initUIWebView
{
    aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 290)];//init and 
create the UIWebView

    aWebView.autoresizesSubviews = YES;
    aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

    [aWebView setDelegate:self];
    NSString *urlAddress = @"http://www.google.com"; // test view is working with url to webpage

    NSURL *url = [NSURL URLWithString:urlAddress];

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

    [aWebView loadRequest:requestObj];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
    [[self view] addSubview:aWebView];  
} 

谢谢
-Code

Thanks -Code

推荐答案

您不必使用 loadRequest 方法,而是必须使用 loadHTMLString 的方法UIWebView
以下代码可以帮助您在<中显示 NSString code> UIWebView

Instead of using the loadRequest method you will have to use the loadHTMLString method of UIWebView The following code might help you in displaying the NSString in UIWebView

NSString *html = @"<html><head></head><body>The Meaning of Life<p>...really is <b>42</b>!</p></body></html>";  
[webView loadHTMLString:html baseURL:nil];

希望这能解决您的问题......

Hope this will resolve your issue...

这篇关于以编程方式创建WebView并在视图中显示NSStrings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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