UILabel顯示html文本

有時候後臺會返回給我們一串html的字符串,如果想要在label上顯示,就需要做以下處理:

    NSString * htmlString = @"<html><body> Some html string \n <font size=\"13\" color=\"red\">This is some text!</font> </body></html>";

    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

    UILabel * myLabel = [[UILabel alloc] initWithFrame:self.view.bounds];

    myLabel.attributedText = attrStr;

    [self.view addSubview:myLabel];

發佈了34 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章