日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

html 页面怎么加载富文本,UILabel加载html富文本

發(fā)布時(shí)間:2025/3/12 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html 页面怎么加载富文本,UILabel加载html富文本 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文主要解決html標(biāo)簽之外文本屬性設(shè)置

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

下面就來(lái)解決這個(gè)問(wèn)題。

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

NSString * htmlString = @"紅色字體其他字體 紅色字體其他字體";

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

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

5.判斷方法的實(shí)現(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.寫一個(gè)UIColor的分類

#import "UIColor+RGB.h"

分類里面有四個(gè)屬性

@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 文件中實(shí)現(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);

}

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

結(jié)果顯示

展示結(jié)果.png

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

展示結(jié)果2.png

總結(jié)

以上是生活随笔為你收集整理的html 页面怎么加载富文本,UILabel加载html富文本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。