如何在iOS的UIWebView上显示HTML内容

5

我是一名有用的助手,可以为您进行文本翻译。以下是需要翻译的内容:

我有一个表单表格视图切换器,应该显示硬编码的HTML页面。 现在我面临两个问题,第一个问题是

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

如下图所示:

enter image description here

如果我将<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];

} 

编辑:

enter image description here

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


1
尝试使用 webView = [[UIWebView alloc] initWithFrame:self.view. bounds ]; 而不是 frame。 - Charan
为什么要使用[html description]而不是只用html - DAS
这不就是你如何将字符串传递给 WebView 的方式吗? - Mord Fustang
1个回答

3
  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"

希望这有所帮助。

似乎大小不重要 :), 这就是她说的哈:) 我已经改了几次,但Xcode没有识别。在问题上添加了图片。 - Mord Fustang
将代码更改为 webView = [[UIWebView alloc] initWithFrame:self.webView.frame]; 并添加 \ 对我有用,谢谢。 - Mord Fustang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接