58

我得到一个 HTML 格式的标题

<p><span style="color: #000000;"><strong>示例</strong></span></p>

我需要在 UILabel 中显示这个 HTML 字符串。颜色代码和字体大小应与 HTML 中的相同。当我将 HTML 字符串转换为 NSString 时,只有文本“Example”出现,而不是颜色。

有什么解决办法吗?

提前谢谢

到目前为止,我正在尝试以下列方式使用 NSAttributedString,但通过这种方式,整个 HTML 正在打印:

UIFont *font = [UIFont systemFontOfSize:14.0];
UIFont *secondFont = [UIFont systemFontOfSize:10.0];

NSMutableDictionary *firstAttributes;
NSMutableDictionary *secondAttributes;

NSDictionary *firstAttributeFont = @{NSFontAttributeName:font};
NSDictionary *secondAttributeFont = @{NSFontAttributeName:secondFont};

[firstAttributes addEntriesFromDictionary:firstAttributeFont];
[secondAttributes addEntriesFromDictionary:secondAttributeFont];

[firstAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor clearColor]}];
[secondAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor clearColor]}];



NSString* completeString = [NSString stringWithFormat:@"%@",strTitle];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]     initWithString:completeString];
[attributedString setAttributes:firstAttributes range:[completeString rangeOfString:strTitle]];
//   [attributedString setAttributes:secondAttributes range:[completeString rangeOfString:self.secondAttributeText]];
Cell.lbl_Title.attributedText = attributedString;
4

7 回答 7

130

对于 iOS7 或更高版本,您可以使用以下命令:

NSString * htmlString = @"<html><body> Some html string </body></html>";
NSAttributedString * attrStr = 
  [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] 
                                   options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
                        documentAttributes:nil error:nil];

UILabel * myLabel = [[UILabel alloc] init];
myLabel.attributedText = attrStr;
于 2014-04-13T19:22:05.483 回答
29

斯威夫特 2

let htmlText = "<p>etc</p>"

if let htmlData = htmlText.dataUsingEncoding(NSUnicodeStringEncoding) {
    do {
        someLabel.attributedText = try NSAttributedString(data: htmlData,
            options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
            documentAttributes: nil)
    } catch let e as NSError {
        print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
    }
}

斯威夫特 3

let htmlText = "<p>etc</p>"

if let htmlData = htmlText.data(using: String.Encoding.unicode) {
    do {
        let attributedText = try NSAttributedString(data: htmlData, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
    } catch let e as NSError {
        print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
    }
}
于 2016-01-26T06:40:11.953 回答
21

斯威夫特 4+

对于 Swift 4 及更高版本,请使用:

guard let data = "foo".data(using: String.Encoding.unicode) else { return }

try? titleLabel.attributedText = 
   NSAttributedString(data: data,
                   options: [.documentType:NSAttributedString.DocumentType.html], 
        documentAttributes: nil)
于 2018-02-06T14:30:13.103 回答
6

我在 UITextView 上这样做如下:

[detailView loadHTMLString:[NSString stringWithFormat:@"<div style='text-align:justify; font-size:13px;font-family:HelveticaNeue;color:#362932;'>%@",[model.dict valueForKey:@"description"]] baseURL:nil];

或者您可以使用 RTLabel 库: https ://github.com/honcheng/RTLabel在标签上显示 html 文本及其格式。

于 2013-11-13T07:25:34.713 回答
5

Swift 扩展 4+

extension UILabel {
  func set(html: String) {
    if let htmlData = html.data(using: .unicode) {
      do {
        self.attributedText = try NSAttributedString(data: htmlData,
                                                     options: [.documentType: NSAttributedString.DocumentType.html],
                                                     documentAttributes: nil)
      } catch let e as NSError {
        print("Couldn't parse \(html): \(e.localizedDescription)")
      }
    }
  }
}
于 2016-11-12T10:16:03.300 回答
4
-(NSString*)convertHtmlPlainText:(NSString*)HTMLString{
    NSData *HTMLData = [HTMLString dataUsingEncoding:NSUTF8StringEncoding];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:HTMLData options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:NULL error:NULL];
    NSString *plainString = attrString.string;

    return plainString;
}

UILabel * htmlLabel = [UILabel alloc] init];
htmlLabel.text = [self convertHtmlPlainText:htmlResponse];
于 2014-08-19T07:31:05.723 回答
0

如果 Html Text 就像

var planeText = "Unseen and not Purchased!"
//setting color to the text
var htmlText = "<font color=\"#f20707\">" + planeText + "</font>"  

我们可以把文字设置成UILabel这样

if let htmlData = htmlText.data(using: String.Encoding.unicode) {
    do {
         let attributedText = try NSAttributedString(data: htmlData,
             options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], 
             documentAttributes: nil)   
         //Setting htmltext to uilable 
         uilable.attributedText = attributedText

       } catch let e as NSError {
         //setting plane text to uilable cause of err
         uilable.text = planeText
         print("Couldn't translate \(htmlText): \(e.localizedDescription) ")
       }
}
于 2017-12-27T06:57:01.010 回答