Robert J. Clegg
Robert J. Clegg

Reputation: 7370

How to display HTML code in UIWebView?

I have HTML code that I get from a web service - it has style tags,(CSS I think) images and that sort of thing. I want to display it in a web view and have the remote images load as well as all the styling.

I've tried to set it like this:

    [self.webView setBackgroundColor:[UIColor clearColor]];


NSMutableString *myString = [NSMutableString stringWithString:self.itemDecription];


[myString replaceOccurrencesOfString:@">" withString:@">" options:NSLiteralSearch range:NSMakeRange(0, [myString length])];
[myString replaceOccurrencesOfString:@"&lt;" withString:@"<" options:NSLiteralSearch range:NSMakeRange(0, [myString length])];

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

However the web view just shows text without any styling and images don't load.

Upvotes: 0

Views: 351

Answers (1)

Deepika
Deepika

Reputation: 263

Try this one:

 NSString *myFile = [[NSBundle mainBundle] pathForResource:@"tag" ofType:@"html"];
 NSString* myString = [NSString stringWithContentsOfFile:myFile encoding:NSUTF8StringEncoding error:nil];
 [webView loadHTMLString:myString baseURL:nil];

Upvotes: 2

Related Questions