在UILabel iphone中显示HTML文本

Khu*_*boo 42 html xcode text uilabel

我从Web服务获得HTML响应以下是我收到的HTML响应

<p><strong>Topic</strong>Gud mrng.</p>
\n<p><strong>Hello Everybody</strong>: How are you.</p>
\n<p><strong>I am fine</strong>: 1 what about you.</p>
Run Code Online (Sandbox Code Playgroud)

我需要在UILabel中显示文本.

请帮忙

Pau*_*ell 95

您可以使用属性文本在没有任何第三方库的情况下执行此操作.我相信它确实接受HTML片段,就像你得到的那样,但是你可能希望将它包装在一个完整的HTML文档中,以便你可以指定CSS:

static NSString *html =
    @"<html>"
     "  <head>"
     "    <style type='text/css'>"
     "      body { font: 16pt 'Gill Sans'; color: #1a004b; }"
     "      i { color: #822; }"
     "    </style>"
     "  </head>"
     "  <body>Here is some <i>formatting!</i></body>"
     "</html>";

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
NSError *err = nil;
label.attributedText =
    [[NSAttributedString alloc]
              initWithData: [html dataUsingEncoding:NSUTF8StringEncoding]
                   options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
        documentAttributes: nil
                     error: &err];
if(err)
    NSLog(@"Unable to parse label text: %@", err);
Run Code Online (Sandbox Code Playgroud)

不简洁,但你可以通过向UILabel添加一个类别来消除混乱:

@implementation UILabel (Html)

- (void) setHtml: (NSString*) html
    {
    NSError *err = nil;
    self.attributedText =
        [[NSAttributedString alloc]
                  initWithData: [html dataUsingEncoding:NSUTF8StringEncoding]
                       options: @{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType }
            documentAttributes: nil
                         error: &err];
    if(err)
        NSLog(@"Unable to parse label text: %@", err);
    }

@end
Run Code Online (Sandbox Code Playgroud)

...

[someLabel setHtml:@"Be <b>bold!</b>"];
Run Code Online (Sandbox Code Playgroud)


Swa*_*ati 15

使用RTLabel库转换HTML文本.我已经好几次使用它了.有用.这是图书馆的链接和示例代码.

https://github.com/honcheng/RTLabel.

希望我帮忙.


小智 13

Swift 4:版本

extension String {
    func htmlAttributedString() -> NSAttributedString? {
        guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
        guard let html = try? NSMutableAttributedString(
            data: data,
            options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html],
            documentAttributes: nil) else { return nil }
        return html
    }
}
Run Code Online (Sandbox Code Playgroud)

Swift 3:版本

extension String {
func htmlAttributedString() -> NSAttributedString? {
    guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
    guard let html = try? NSMutableAttributedString(
        data: data,
        options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
        documentAttributes: nil) else { return nil }
    return html
    }
}
Run Code Online (Sandbox Code Playgroud)

Swift 2:版本

extension String {
        func htmlAttributedString() -> NSAttributedString? {
            guard let data = self.dataUsingEncoding(NSUTF16StringEncoding, allowLossyConversion: false) else { return nil }
            guard let html = try? NSMutableAttributedString(
              data: data, 
              options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
              documentAttributes: nil) else { return nil }
            return html
        }
}
Run Code Online (Sandbox Code Playgroud)

用它作为:

label.attributedText = yourStringVar.htmlAttributedString()
Run Code Online (Sandbox Code Playgroud)


Pau*_*l B 5

斯威夫特4

我宁愿建议使用失败的便利性init扩展NSAttributedString。String本质上不负责制作NSAttributedString

extension NSAttributedString {
     convenience init?(html: String) {
        guard let data = html.data(using: String.Encoding.unicode, allowLossyConversion: false) else {
            return nil
        }
        guard let attributedString = try? NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) else {
            return nil
        }
        self.init(attributedString: attributedString)
    }
}
label.attributedText = NSAttributedString(html: "<span> Some <b>bold</b> and <a href='#/userProfile/uname'> Hyperlink </a> and so on </span>")
Run Code Online (Sandbox Code Playgroud)