关键字高亮(富文本)

作者: 游城十代2dai | 来源:发表于2016-08-14 14:40 被阅读77次

关键字高亮, 实际上就是对富文本的操作, 富文本的属性大致如下:

 阴影
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowBlurRadius = 1.0;// 模糊程度
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(1, 3);

NSFontAttributeName               字体
NSKernAttributeName               字间距[NSNumber]
NSParagraphStyleAttributeName     段落 [NSParagraphStyle ]
NSBackgroundColorAttributeName    背景色
NSForegroundColorAttributeName    字体色
NSLigatureAttributeName           连字符[NSNumber 0 表示没有连体字符 1 默认的连体字符 2表示使用所有连体符 默认值为1]
NSUnderlineStyleAttributeName     下划线
NSStrikethroughStyleAttributeName 删除线[不指定的情况下删除线颜色即为字体色]
NSShadowAttributeName             阴影 [创建NSShadow设置其偏移量(CGSizeMake)颜色及模糊程度 要配合下面3个使用]

NSObliquenessAttributeName        斜体
NSExpansionAttributeName          扁平化
NSVerticalGlyphFormAttributeName  [整数0为横排文本 1为竖排文本]
NSStrokeColorAttributeName        描边颜色 [同时设置描边属性 之前的字体色失效]
NSStrokeWidthAttributeName        描边宽度 [小数默认为0为负数时改变描边和填充宽度 对于空心字 通常为3.0]

我们经常把富文本的属性功能放到一个字典中:

NSDictionary *dicA = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],          // 字体
                       NSForegroundColorAttributeName:[UIColor blackColor],           // 字体色
                       NSKernAttributeName:@5.0,                                      // 字间距
                       NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle),   // 删除线
                       NSStrokeColorAttributeName:[UIColor blueColor],                // 描边颜色
                       NSStrokeWidthAttributeName:@2.0,
                       NSVerticalGlyphFormAttributeName:@(0)};

对一个关键字进行富文本属性赋值:

 //设置关键字属性
 - (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs range:(NSRange)range;

举一个我做项目时候的例子(前方高能上图)

当前多少直播那里就是富文本的一种用法

代码我就简单实现一下:

// 先创建一个 UILabel 它的 frame 随意给就好
UILabel *liveContent = [[UILabel alloc] initWithFrame:CGRectMake(180, 0, 300, 30)];

// 给字体设置个颜色 
liveContent.textColor = [UIColor grayColor];

// 根据想要改变的位置, 先给出一个改变位置的字符串, 比如我这里需要的是数字
NSString *count = @"3984";

// 给文本一个内容
liveContent.text = [NSString stringWithFormat:@"当前%@个直播, 进去看看", count];
       
// 创建富文本 (可变的) NSMutableAttributedString 并把想要修改的原字符串填入
NSMutableAttributedString *temp = [[NSMutableAttributedString alloc] initWithString:liveContent.text];
       
// 获取改变部分在原字符串中的位置 
NSRange range = [liveContent.text rangeOfString:count];
        
// 设置富文本属性(切记字典的最后一组属性, 不要有逗号)
NSDictionary *dicA = @{
                           NSFontAttributeName:[UIFont boldSystemFontOfSize:20],
                           NSForegroundColorAttributeName:[UIColor colorWithRed:255.0/255.0 green:115.0/255.0 blue:156.0/255.0 alpha:255.0/255.0]
                           };

// 添加属性到相应位置
[temp setAttributes:dicA range:range];
    
// 将富文本给 label 的 attibutedText 属性, 这里用的是赋值符号=给予的
liveContent.attributedText = attributedString;

PS: 只有单一属性的话, 没有必要写成字典

// 可以直接用 addAttribute: value: 这个方法添加单一属性
 [temp addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:115.0/255.0 blue:156.0/255.0 alpha:255.0/255.0] range:range];

// 赋值写法不一, 看个人喜好, 这里再给出一个 KVC 的方式赋值        
[liveContent setValue:temp forKey:@"attributedText"];

相关文章

网友评论

    本文标题:关键字高亮(富文本)

    本文链接:https://www.haomeiwen.com/subject/njpysttx.html