+ (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;
}
使用富文本解析html
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來末誓,“玉大人扯俱,你說我怎么就攤上這事±瑁” “怎么了迅栅?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長晴玖。 經(jīng)常有香客問我读存,道長,這世上最難降的妖魔是什么呕屎? 我笑而不...
- 正文 為了忘掉前任让簿,我火速辦了婚禮,結(jié)果婚禮上秀睛,老公的妹妹穿的比我還像新娘尔当。我一直安慰自己,他們只是感情好蹂安,可當我...
- 文/花漫 我一把揭開白布椭迎。 她就那樣靜靜地躺著,像睡著了一般田盈。 火紅的嫁衣襯著肌膚如雪畜号。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼视卢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了廊驼?” 一聲冷哼從身側(cè)響起据过,我...
- 正文 年R本政府宣布茂蚓,位于F島的核電站壕鹉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏聋涨。R本人自食惡果不足惜晾浴,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望牍白。 院中可真熱鬧脊凰,春花似錦、人聲如沸茂腥。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽础芍。三九已至杈抢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間仑性,已是汗流浹背惶楼。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 參考:android 開發(fā)--抓取網(wǎng)頁解析網(wǎng)頁內(nèi)容的若干方法(網(wǎng)絡(luò)爬蟲)(正則表達式) 網(wǎng)頁有兩種格式巩剖,一種是xm...
- 【知識點】 一. 數(shù)據(jù)的網(wǎng)絡(luò)請求 【注】大多數(shù)APP都是依托于服務(wù)器的,由服務(wù)器負責數(shù)據(jù)的管理交互钠怯,分發(fā)佳魔,篩選等等...
- TFHpple是一個XML/HTML解析框架,我們可以用來解析從后臺發(fā)送過來的HTML數(shù)據(jù)晦炊。如果要在項目中使用這個...
- 本項目由微信小程序開發(fā)論壇-WeAppDev http://weappdev.com/ 會員開發(fā) 原文地址: ht...
- HTML文檔加載邊解析 之前一直以為html是加載完之后才由html解析器進行解析的断国,學習了瀏覽器原理之后贤姆,才知道...