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

By simon at 2018-02-07 • 0人收藏 • 166人看过

我正在以HTML格式获得一个标题

<span style =“color:#000000;”> 示例 我需要表明这一点UILabel中的HTML字符串。颜色代码和字体大小 应该与HTML中的相同。当我转换的HTML串 到一个NSString中,只有文本“Example”来了,而不是颜色。 有没有解决办法? 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;

6 个回复 | 最后更新于 2018-02-07
2018-02-07   #1

我在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来 DISPLAy html文本以及其在标签上的格式。

2018-02-07   #2

对于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;

2018-02-07   #3

    -(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];

2018-02-07   #4

* Swift 2 *

let htmlText = "

etc

" 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) ") } }
* Swift 3 *
let htmlText = "

etc

" 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) ") } }

2018-02-07   #5

扩展为Swift 3

extension UILabel {
    func from(html: String) {
        if let htmlData = html.data(using: String.Encoding.unicode) {
            do {
                self.attributedText = try NSAttributedString(data: htmlData,
                                                             options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
                                                             documentAttributes: nil)
            } catch let e as NSError {
                print("Couldn't parse \(html): \(e.localizedDescription)")
            }
        }
    }
}

2018-02-07   #6

如果HTML文本是

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) ")
       }
}

登录后方可回帖

Loading...