使用富文本解析html

+ (NSMutableAttributedString *)formatText:(NSString *)text {
    NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *options = @{NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute :@(NSUTF8StringEncoding) };
    NSError *error = nil;
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:&error];

    if (error) {
        NSLog(@"HTML 解析錯誤: %@", error);
        return [[NSMutableAttributedString alloc] init];
    }
    
    // 創(chuàng)建可變富文本
    NSMutableAttributedString *mutableAttributedString = [attributedString mutableCopy];
    // 遍歷整個富文本的范圍袋坑,移除背景色屬性
    [mutableAttributedString enumerateAttribute:NSBackgroundColorAttributeName
                                        inRange:NSMakeRange(0, mutableAttributedString.length)
                                        options:0
                                     usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        if (value) {
            [mutableAttributedString removeAttribute:NSBackgroundColorAttributeName range:range];
        }
    }];
    
    // 設(shè)置字體
    [mutableAttributedString beginEditing];
    [mutableAttributedString enumerateAttribute:NSFontAttributeName
                                        inRange:NSMakeRange(0, mutableAttributedString.length)
                                        options:0
                                     usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        if (value) {
            UIFont *newFont = [UIFont systemFontOfSize:14];
            [mutableAttributedString addAttribute:NSFontAttributeName value:newFont range:range];
        }
    }];
    [mutableAttributedString endEditing];
    
    // 設(shè)置字體行間距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 1.5;
    [mutableAttributedString addAttribute:NSParagraphStyleAttributeName
                             value:paragraphStyle
                             range:NSMakeRange(0, attributedString.length)];
    
    // 設(shè)置文本顏色
    [mutableAttributedString enumerateAttribute:NSBackgroundColorAttributeName
                                        inRange:NSMakeRange(0, mutableAttributedString.length)
                                        options:0
                                     usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        if (value) {
            [mutableAttributedString removeAttribute:NSBackgroundColorAttributeName range:range];
        }
    }];
    [mutableAttributedString addAttribute:NSForegroundColorAttributeName
                             value:hexColor(333333)
                             range:NSMakeRange(0, attributedString.length)];

    return mutableAttributedString;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末怖现,一起剝皮案震驚了整個濱河市渊额,隨后出現(xiàn)的幾起案子濒蒋,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件葛家,死亡現(xiàn)場離奇詭異,居然都是意外死亡泌类,警方通過查閱死者的電腦和手機癞谒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來末誓,“玉大人扯俱,你說我怎么就攤上這事±瑁” “怎么了迅栅?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長晴玖。 經(jīng)常有香客問我读存,道長,這世上最難降的妖魔是什么呕屎? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任让簿,我火速辦了婚禮,結(jié)果婚禮上秀睛,老公的妹妹穿的比我還像新娘尔当。我一直安慰自己,他們只是感情好蹂安,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布椭迎。 她就那樣靜靜地躺著,像睡著了一般田盈。 火紅的嫁衣襯著肌膚如雪畜号。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天允瞧,我揣著相機與錄音简软,去河邊找鬼。 笑死述暂,一個胖子當著我的面吹牛痹升,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播畦韭,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼视卢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了廊驼?” 一聲冷哼從身側(cè)響起据过,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎妒挎,沒想到半個月后绳锅,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡酝掩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年鳞芙,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片期虾。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡原朝,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出镶苞,到底是詐尸還是另有隱情喳坠,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布茂蚓,位于F島的核電站壕鹉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏聋涨。R本人自食惡果不足惜晾浴,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望牍白。 院中可真熱鬧脊凰,春花似錦、人聲如沸茂腥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽础芍。三九已至杈抢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間仑性,已是汗流浹背惶楼。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留诊杆,地道東北人歼捐。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像晨汹,于是被迫代替她去往敵國和親豹储。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容