【问题标题】:How to show HTML text from API on the iPhone?如何在 iPhone 上显示来自 API 的 HTML 文本?
【发布时间】:2011-05-07 12:05:21
【问题描述】:

解释我的情况的最佳示例是使用博客文章。假设我有一个 UITableView 加载了我从 API 获得的博客文章的标题。当我点击一行时,我想显示详细的博客文章。

这样做时,API 会返回几个字段,包括“帖子正文”(即 HTML 文本)。我的问题是,我应该用什么来显示它以便它显示为格式化的 HTML?我应该为此使用 UIWebView 吗?我不确定当您从字面上查看网页时是否使用 UIWebView(例如使用 URL 或其他内容对其进行初始化),或者您是否可以将其传递给 HTML 字符串并且它会正确格式化。

此页面上将显示其他几个字段,例如标题、类别、作者等。我只是使用 UILabels 来处理这些,所以没有问题。但我不知道如何处理 HTML 块。顺便说一句,我正在以编程方式完成所有这些工作。

如果你看不出来,我对 iOS 开发还比较陌生,只有大约 2-3 周的时间,没有 obj-c 背景。因此,如果 UIWebView 是正确的方法,我也会很感激任何“陷阱!”笔记,如果有的话。

【问题讨论】:

    标签: ios iphone objective-c uiwebview uitextview


    【解决方案1】:

    正如 David Liu 所说,UIWebview 是要走的路。我会推荐一些单独构建 HTML 字符串,然后将其传递给 UIWebView。另外,我会使用[webView setBackgroundColor:[UIColor clearColor]] 使背景透明,这样您就可以更轻松地使事物看起来像他们应该的样子。

    这是一个代码示例:

    - (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 content here"];
        [html appendString:@"</body></html>"];
    
        //instantiate the web view
        UIWebView *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];
    
    }
    

    注意:

    使用“NSMutableString”的好处是您可以通过整个解析操作继续构建字符串,然后将其传递给“UIWebView”,而“NSString”一旦创建就无法更改。

    【讨论】:

    • 谢谢,正是我想要的。
    • viewDidLoad 会是什么样子?
    • 注意 UIWebView 的内存使用
    • 这对可访问性来说很糟糕。 UIWebviews 无法轻易识别设备的辅助功能文本大小。
    【解决方案2】:
       self.textLbl.attributedText = [[NSAttributedString alloc] initWithData:    [@"html-string" dataUsingEncoding:NSUnicodeStringEncoding]
                                                                              options:@{     NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType
                                                                                         } documentAttributes:nil error:nil];
    

    【讨论】:

      【解决方案3】:
      NSString *strForWebView = [NSString stringWithFormat:@"<html> \n"
            "<head> \n"
            "<style type=\"text/css\"> \n"
            "body {font-family: \"%@\"; font-size: %@; height: auto; }\n"
            "</style> \n"
            "</head> \n"
            "<body>%@</body> \n"
            "</html>", @"helvetica", [NSNumber numberWithInt:12], ParameterWhereYouStoreTextFromAPI];
      
      
       [self.webview loadHTMLString:strForWebView baseURL:nil];
      

      我正在使用此代码甚至设置 webview 文本的字体并将我的 ivar 'ParameterWhereYouStoreTextFromAPI' 传递给我存储从 api 获得的文本。

      【讨论】:

        【解决方案4】:

        在原始 HTML(文本样式、p/br 标记)的特殊情况下,您还可以将 UITextView 与未记录的属性一起使用:

        -[UITextView setValue:@"<b>bold</b>" forKey:@"contentToHTMLString"]
        

        尽管它没有记录在案,但它已在我知道的许多应用程序中使用,到目前为止还没有引起过一次拒绝。

        【讨论】:

        • 从技术上讲,setValue:forKey: 不是无证的。
        • contentToHTMLString 键未记录。
        【解决方案5】:

        你可以使用 UIWebView 的 –loadHTMLString:baseURL: 方法。

        参考链接:here

        【讨论】:

          【解决方案6】:

          Moshe 的回答很好,但是如果你想要透明的 webview,你需要将属性 opaque 设置为 FALSE。

          您可以查看:How to make a transparent UIWebVIew

          【讨论】:

            【解决方案7】:

            您可以通过移除 HTML/JS 文本来显示它,例如 (align,centre,br>)。如果您的目标是 iOS7.0 及更高版本,请使用此方法。

                NSString *htmlFile;
            
                htmlFile=[array valueForKey:@"results"];
            
                NSAttributedString *attr = [[NSAttributedString alloc] initWithData:[htmlFile dataUsingEncoding:NSUTF8StringEncoding]options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}documentAttributes:nil error:nil];
                    NSLog(@"html: %@", htmlFile);
                    NSLog(@"attr: %@", attr);
                    NSLog(@"string: %@", [attr string]);
                    NSString *finalString = [attr string];
            
                    [webView loadHTMLString:[finalString description] baseURL:nil];
            

            【讨论】:

              【解决方案8】:

              我的场景:我在视图控制器中有一个 Textview,并且必须在 HTML 格式的 textView 中显示数据。

              斯威夫特 3:

              func detectUrlInText() {
              
              let attrStr = try! NSAttributedString(
                          data: "<b><i>\(Ldesc)</i></b>".data(using: String.Encoding.unicode, allowLossyConversion: true)!,
                          options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                          documentAttributes: nil)
              
               desc.attributedText = attrStr
              
               desc.font = UIFont(name: "CALIBRI", size: 14)
              
              }
              // Ldesc is the string which gives me the data to put in the textview. desc is my UITextView.
              :)
              

              【讨论】:

                猜你喜欢
                • 2018-07-27
                • 2017-10-25
                • 1970-01-01
                • 2017-07-08
                • 2013-03-30
                • 1970-01-01
                • 1970-01-01
                • 2021-09-12
                • 1970-01-01
                相关资源
                最近更新 更多