How to display html content on UIWebView IOS

Mord Fustang picture Mord Fustang · Oct 8, 2012 · Viewed 15.5k times · Source

I have a form sheet segue that should display hard-coded html page. Now I have a two part problem first problem is

 <CENTER><H1>A Simple Sample Web Page</H1><H4>By Frodo</H4>
<H2>Demonstrating a few HTML features</H2></CENTER>

is displayed like

enter image description here

I can change <CENTER>tag to <left> in that case it works but it limits my options. How can I display this content In the middle of the screen without alignment issues?

Second part of the question is How can I embed a color or a link to content. On line ""FFFFFF">\n" I get the error that says Expected ':' and on line "<a href="http://somegreatsite.com">\n" Half of the line is green which is seems like comment on the screen that means it will not executed by XCode

My full code is like :

- (void) createWebViewWithHTML{
    //create the string
    NSMutableString *html = [NSMutableString stringWithString: @"<html><head><title></title></head><body style=\"background:transparent;\">"];

    //continue building the string
    [html appendString:@"<BODY BGCOLOR="
     ""FFFFFF">\n"
     "<CENTER><IMG SRC="clouds.jpg" ALIGN="BOTTOM">
     "</CENTER> \n"
     "<HR>\n"
     "<a href="http://somegreatsite.com">\n"
     "Link Name</a>\n"

     "is a link to another nifty site\n"

     "<H1>This is a Header</H1>\n"

     "<H2>This is a Medium Header</H2>\n"

     "Send me mail at <a href="mailto:[email protected]">\n"

     "[email protected]</a>.\n"

     "<P> This is a new paragraph!\n"

     "<P> <B>This is a new paragraph!</B>\n"

     "<BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>\n"

     "<HR>\n"];
    [html appendString:@"</body></html>"];

    //instantiate the web view
    webView = [[UIWebView alloc] initWithFrame:self.view.frame];


    //make the background transparent
    [webView setBackgroundColor:[UIColor clearColor]];

    //pass the string to the webview
    [webView loadHTMLString:[html description] baseURL:nil];

    //add it to the subview
    [self.view addSubview:webView];

} 

Edit:: Added story board picture

enter image description here

So how can I make correct alignment and embed color/link correctly?

Answer

FD_ picture FD_ · Oct 8, 2012
  1. Check if your UIWebView has the right size. It seems to be bigger than the screen.

  2. Place a \ before any " in your hard-coded string. e.g.:

    NSString *testString="This is a \"Test\"";
    

    results in

    This is a "Test"

Hope this helps