如何在iOS中的UILabel上显示HTML字符串?

vas*_*dev 50 ios

我正在获得HTML格式的标题

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

我需要在UILabel中显示这个HTML字符串.颜色代码和字体大小应与HTML中的相同.当我将HTML字符串转换为NSString时,只有文本"示例"即将到来,而不是颜色.

有什么解决方案吗?

Thnx提前

直到现在我尝试通过以下方式使用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;
Run Code Online (Sandbox Code Playgroud)

san*_*ana 126

对于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;
Run Code Online (Sandbox Code Playgroud)

  • 它有效,但我怎样才能在UILabel上发现"触摸"事件? (3认同)
  • 谢谢,但我只想在html内容的链接上捕捉"触摸"事件,而不是所有的标签 (2认同)

Bas*_*Zen 28

斯威夫特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) ")
    }
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特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) ")
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 在 Swift 4 中不起作用无法将“NSAttributedString.DocumentAttributeKey”类型的值转换为预期的字典键类型“NSAttributedString.DocumentReadingOptionKey” (3认同)

Jak*_*lář 18

Swift 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)
Run Code Online (Sandbox Code Playgroud)


mak*_*mak 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];
Run Code Online (Sandbox Code Playgroud)

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

  • 不要使用loadHTMLString,它是一个私有api. (6认同)

Ork*_*nov 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)")
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)