UILabel加載html富文本

本文主要解決html標(biāo)簽之外文本屬性設(shè)置
當(dāng)APP里面有搜索的需求的時候,產(chǎn)品可能會要求關(guān)鍵字顯示特殊顏色或者字體。其中一種可能性是服務(wù)器返回的數(shù)據(jù)是帶有html標(biāo)簽的字符串,那么該怎么解決?當(dāng)標(biāo)簽之外的其他字體也需要設(shè)置不同格式,又要怎么解決?

下面就來解決這個問題。

1.一個帶有html標(biāo)簽的字符串

NSString * htmlString = @"<font color=red>紅色字體</font>其他字體 <font color=red>紅色字體</font>其他字體";

2.設(shè)置自己想要字體屬性

NSDictionary *dic = @{NSForegroundColorAttributeName: [UIColor grayColor],
                          NSBackgroundColorAttributeName: [UIColor clearColor],
                          NSFontAttributeName:  [UIFont systemFontOfSize:15]};

3.顯示html格式的文本

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

第三步就是將html格式的文本轉(zhuǎn)化成字符串

4.解決除了html標(biāo)簽之外的字體設(shè)置

void (^block)(NSDictionary*,NSRange,BOOL*) = ^(NSDictionary *attrs, NSRange range, BOOL *stop){
        UIColor *color =  attrs[NSForegroundColorAttributeName];
        UIColor *colorRed = [UIColor redColor];
        if (color && ![self isTheSameColor2:color  anotherColor:colorRed]) {
            [nameText  addAttributes:dic range: range];
        }  else{
            NSMutableDictionary   *dicM  = [attrs mutableCopy];
            dicM[NSFontAttributeName ] = [UIFont systemFontOfSize:15];
            [nameText  addAttributes:dicM range: range];
        }
    };

[nameText enumerateAttributesInRange: NSMakeRange(0, nameText.length) options: NSAttributedStringEnumerationReverse usingBlock: block];

第四步就是解決其他文本屬性值的改變,這里只做了通過顏色來區(qū)別是html標(biāo)簽文本還是其他文本,標(biāo)簽里面是redColor,那么這里的判斷條件自然就是redColor,是通過isTheSameColor2: anotherColor:這個方法進(jìn)行判斷
5.判斷方法的實現(xiàn)

 - (BOOL) isTheSameColor2:(UIColor*)color1 anotherColor:(UIColor*)color2 {
    if ([color1 red] == [color2 red] &&  [color1 green] == [color2 green] &&
        [color1 blue] == [color2 blue] &&[color1 alpha] == [color2 alpha] ) {
        return YES;
    }  else {
        return NO;
    }
}

6.寫一個UIColor的分類
#import "UIColor+RGB.h"
分類里面有四個屬性

@interface UIColor (RGB)
@property (nonatomic, readonly) CGFloat red;
@property (nonatomic, readonly) CGFloat green;
@property (nonatomic, readonly) CGFloat blue;
@property (nonatomic, readonly) CGFloat alpha;
@end

在 .m 文件中實現(xiàn)

- (CGFloat)red {
    CGFloat r = 0, g, b, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return r;
}
- (CGFloat)green {
    CGFloat r, g = 0, b, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return g;
}
- (CGFloat)blue {
    CGFloat r, g, b = 0, a;
    [self getRed:&r green:&g blue:&b alpha:&a];
    return b;
}
- (CGFloat)alpha {
    return CGColorGetAlpha(self.CGColor);
}

這個分類的作用在于比對顏色值,便于在第五步里面進(jìn)行顏色的判斷

結(jié)果顯示


展示結(jié)果.png

但是如果只執(zhí)行到第三步,那么紅色字體之外的顏色就是黑色


展示結(jié)果2.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • HTML標(biāo)簽解釋大全 一、HTML標(biāo)記 標(biāo)簽:!DOCTYPE 說明:指定了 HTML 文檔遵循的文檔類型定義(D...
    米塔塔閱讀 3,539評論 1 41
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,204評論 1 92
  • 本文主要是起筆記的作用,內(nèi)容來自慕課網(wǎng). 認(rèn)識CSS樣式 CSS全稱為“層疊樣式表 (Cascading Styl...
    0o凍僵的企鵝o0閱讀 2,754評論 0 30
  • 以前的我們,寧愿慢慢的等待結(jié)果來臨。 也不愿意付出努力,讓結(jié)果變成我們想要的 長大的我們,慢慢相信通過自己的努力。...
    深久不彼閱讀 234評論 0 0
  • 1.精要主義核心: 找出最重要的事情,把精力花在這上面,排除阻礙,高效完成 找出核心:花時間做加法,跳起來,連起來...
    小多媛媛閱讀 290評論 0 0

友情鏈接更多精彩內(nèi)容