开发者

Creating a WebView programmatically and displaying NSStrings inside the view

开发者 https://www.devze.com 2023-01-22 18:24 出处:网络
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-text

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.

Is it possible to get these displayed inside the WebView? I'm guessing I need t开发者_StackOverflowo 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];  
} 

Thanks -Code


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号