UILabel加载html富文本

本文主要解决html标签之外文本属性设置
当APP里面有搜索的需求的时候,产品可能会要求关键字显示特殊颜色或者字体。其中一种可能性是服务器返回的数据是带有html标签的字符串,那么该怎么解决?当标签之外的其他字体也需要设置不同格式,又要怎么解决?

下面就来解决这个问题。

1.一个带有html标签的字符串

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

2.设置自己想要字体属性

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格式的文本转化成字符串

4.解决除了html标签之外的字体设置

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

第四步就是解决其他文本属性值的改变,这里只做了通过颜色来区别是html标签文本还是其他文本,标签里面是redColor,那么这里的判断条件自然就是redColor,是通过isTheSameColor2: anotherColor:这个方法进行判断
5.判断方法的实现

 - (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 文件中实现

- (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);
}

这个分类的作用在于比对颜色值,便于在第五步里面进行颜色的判断

结果显示


展示结果.png

但是如果只执行到第三步,那么红色字体之外的颜色就是黑色


展示结果2.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • HTML标签解释大全 一、HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(D...
    米塔塔阅读 3,329评论 1 41
  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,809评论 1 92
  • 本文主要是起笔记的作用,内容来自慕课网. 认识CSS样式 CSS全称为“层叠样式表 (Cascading Styl...
    0o冻僵的企鹅o0阅读 2,664评论 0 30
  • 以前的我们,宁愿慢慢的等待结果来临。 也不愿意付出努力,让结果变成我们想要的 长大的我们,慢慢相信通过自己的努力。...
    深久不彼阅读 217评论 0 0
  • 1.精要主义核心: 找出最重要的事情,把精力花在这上面,排除阻碍,高效完成 找出核心:花时间做加法,跳起来,连起来...
    小多媛媛阅读 263评论 0 0