如何在UIWebView IOS上显示html内容

Mor*_*ang 5 html objective-c uiwebview nsmutablestring ios

我有一个表单segue应该显示硬编码的html页面.现在我有两个问题,第一个问题是

 <CENTER><H1>A Simple Sample Web Page</H1><H4>By Frodo</H4>
<H2>Demonstrating a few HTML features</H2></CENTER>
Run Code Online (Sandbox Code Playgroud)

显示为

在此输入图像描述

我可以更改<CENTER>标签,<left>在这种情况下它可以工作,但它限制了我的选择.如何显示此内容在屏幕中间没有对齐问题?

问题的第二部分是如何嵌入内容的颜色或链接.在线 ""FFFFFF">\n"我得到的错误说明Expected ':'并在线"<a href="http://somegreatsite.com">\n"半线是绿色,这似乎是屏幕上的评论,这意味着它不会被XCode执行

我的完整代码如下:

- (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:support@yourcompany.com">\n"

     "support@yourcompany.com</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];

} 
Run Code Online (Sandbox Code Playgroud)

编辑::添加故事板图片

在此输入图像描述

那么如何才能正确对齐并正确嵌入颜色/链接?

FD_*_*FD_ 3

  1. 检查您的UIWebView尺码是否合适。看起来比屏幕还大。

  2. 在硬编码字符串中的任何“之前放置一个 \。例如:

    NSString *testString="This is a \"Test\"";
    
    Run Code Online (Sandbox Code Playgroud)

    结果是

    这是一个测试”

希望这可以帮助