Thursday, 17 October 2013

Load html in webview

In Ios Data can be displayed in webview and it can be done  three ways:

1>you can display data from url

you can load url this way

NSURL *websiteUrl = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];

[myWebView loadRequest:urlRequest]; 

2>you can display local html file 

for that you can use this code

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

3>you can also display text in webview

we can load plain text in webview this way

we can load html string in which we can lapply font style,size,text alignment ,word wrap,font color

if your string contains \r\n and \n then you can replace it via <br>tag

description = [description stringByReplacingOccurrencesOfString:@"\r\n"

                                                               withString:@"<br>"];
description = [description stringByReplacingOccurrencesOfString:@"\\n"
                                                                     withString:@"<br>"]


NSString *strForhtmldata=[NSString stringWithFormat:@"<html><head><style>body{background-color:transparent;}</style> </head><body <div style=\"width: 260px; word-wrap: break-word\"><p style=\"text-align:justify; font-family:HelveticaNeue-Regular;font-size:13px;color:#767573; line-height:18px;\">%@</p></body></html>",description];
[webView loadHTMLString:strForhtmldata baseURL:nil];


No comments:

Post a Comment