【问题标题】:How to display html content on UIWebView IOS如何在 UIWebView IOS 上显示 html 内容
【发布时间】:2012-10-08 15:06:59
【问题描述】:

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

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

显示方式

我可以将 &lt;CENTER&gt;tag 更改为 &lt;left&gt; 在这种情况下它可以工作,但它限制了我的选择。如何在没有对齐问题的情况下在屏幕中间显示此内容?

问题的第二部分是如何嵌入颜色或指向内容的链接。在""FFFFFF"&gt;\n" 线上,我收到Expected ':' 的错误消息,在"&lt;a href="http://somegreatsite.com"&gt;\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];

} 

编辑:: 添加故事板图片

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

【问题讨论】:

  • 尝试给 webView = [[UIWebView alloc] initWithFrame:self.view。 界限 ];而不是框架
  • 为什么是[html description] 而不仅仅是html
  • 这不是你将字符串传递给 webview 的方式吗?

标签: html objective-c ios uiwebview nsmutablestring


【解决方案1】:
  1. 检查您的UIWebView 的大小是否正确。好像比屏幕还大。

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

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

    结果

    这是一个“测试”

希望对你有帮助

【讨论】:

  • 似乎大小无关紧要:),这就是她所说的哈:) 我已经更改了几次,但 Xcode 无法识别。在问题上添加图片
  • 将代码更改为 webView = [[UIWebView alloc] initWithFrame:self.webView.frame]; 并添加 \ 对我有用,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 1970-01-01
  • 2020-04-09
  • 2014-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多