美文网首页
OC: NSMutableAttributedString 链式

OC: NSMutableAttributedString 链式

作者: LiYaoPeng | 来源:发表于2018-08-27 19:22 被阅读0次
屏幕快照 2018-08-27 下午7.22.53.png

demo地址

链式调用

思路

  1. 定义一些相应的返回值为 NSMutableAttributedString(^)(id value) 的方法

例子

注意这里的实现,需要用weakSelf 若引用

  1. foregroundColor
/// 设置text foregroundColor
- (NSMutableAttributedString *(^)(UIColor *)) color;

- (NSMutableAttributedString *(^)(UIColor *)) color {
    __weak typeof (self) weakSelf = self;
    return ^(UIColor *color) {
        [weakSelf foregroundColor:color];
        return weakSelf;
    };
}

- (instancetype) foregroundColor: (UIColor *)color {
    [self addAttribute:NSForegroundColorAttributeName value:color range:[self getRange]];
    return self;
}
  1. font
/// 设置font
- (NSMutableAttributedString *(^)(UIFont *)) font;

- (NSMutableAttributedString *(^)(UIFont *)) font {
    __weak typeof (self) weakSelf = self;
    return ^(UIFont *font) {
        [weakSelf font:font];
        return weakSelf;
    };
}

- (instancetype) font: (UIFont *)font {
    [self addAttribute:NSFontAttributeName value:font range:[self getRange]];
    return self;
}

调用:

[NSMutableAttributedString new]
.color([UIColor redColor])
.font([UIFont systemFontOfSize:10]);

根据range做AttributedString属性操作

  1. 根据range 提取出subString,并mutableCopy
  2. 闭包提供接口,外面来修改subString
  3. 把设置好的subString 替换掉以前位置的subString
/// 设置 AttributedString 指定range的属性
- (instancetype) setupInRange:(NSRange)range andCallBack: (void(^)(NSMutableAttributedString *attributedStr))callBack;

- (instancetype) setupInRange:(NSRange)range andCallBack: (void(^)(NSMutableAttributedString *attributedStr))callBack{
    [self setRange:range];
    NSMutableAttributedString *str = [[self attributedSubstringFromRange:range] mutableCopy];
    if(callBack) {
        callBack(str);
    }
    [self replaceCharactersInRange:range withAttributedString:str];
    [self setRange: NSMakeRange(0, self.length)];
    return self;
}

调用

[attributedString setupInRange:obj.range andCallBack:^(NSMutableAttributedString *attributedStr) {
                
                attributedStr
                .color(ruler.color)
                .font([UIFont systemFontOfSize:30])
                .isLigature(true)
                .kern(12)
                .backgroundColor([UIColor lightGrayColor])
                .strikethrough(lineStyle, [UIColor blueColor],@0)
                .stroke(1,[UIColor blueColor])
                .shadow(shadow)
                .addBottomLine(lineStyle,[UIColor redColor])
                .registerSingleClick(^{
                    attributedStringM.font([UIFont systemFontOfSize:20]);
                    [weakSelf dismissViewControllerAnimated:true completion:nil];
                });
            }];

查询特定字符

  1. 定义modelAttributedStrFiltrateRuler,用来储存相应的查询规则与查询结果

/// 正则表达式
@property (nonatomic,copy) NSString *expressionString;

/// 所有的符合条件的 range集合
@property (nonatomic,strong) NSMutableArray <NSValue *>*resultRangeArray;

/// NSTextCheckingResult
@property (nonatomic,strong) NSArray <NSTextCheckingResult *>*textCheckingResultArray;
@property (nonatomic,strong) UIColor *color;
  1. 创建AttributedString 查询分类 NSString+FiltrateRuler
/// 根据多个 AttributedStrFiltrateRuler 查询匹配
- (NSArray <AttributedStrFiltrateRuler *>*) filtrates: (NSArray <AttributedStrFiltrateRuler *>*)filtrateRulerArray {
    
    NSMutableArray *rulerArray = [filtrateRulerArray mutableCopy];
    [rulerArray enumerateObjectsUsingBlock:^(AttributedStrFiltrateRuler * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self filtrate:obj];
    }];
    return rulerArray;
}

/// 查询匹配

- (AttributedStrFiltrateRuler *) filtrate: (AttributedStrFiltrateRuler *)ruler {
    
    NSString *string = [self copy];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:ruler.expressionString options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray <NSTextCheckingResult *>* matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    
    NSMutableArray <NSValue *> *resultRangeArray = [[NSMutableArray alloc]init];
    
    for (NSTextCheckingResult *match in matches) {
        [resultRangeArray addObject: [NSValue valueWithRange:match.range]];
    }
    
    ruler.resultRangeArray = resultRangeArray;
    ruler.textCheckingResultArray = matches;
    return ruler;
}

demo地址

相关文章

  • OC: NSMutableAttributedString 链式

    demo地址 链式调用 思路 定义一些相应的返回值为 NSMutableAttributedString(^)(i...

  • 富文本带点击事件

    app开发场景中,经常会使用到富文本,常规做法使用NSMutableAttributedString设置OC版本代...

  • 像swift一样书写OC代码OC链式编程实践

    链式编程特点 链式编程 = 点语法 事物 串联 同样的hello word代码 OC和swift调用函数时候最大的...

  • oc链式赋值

    偶然发现同事写的代码里面有两个连续的赋值,甚是懵逼,查了下链式赋值要么是oc的链式编程要么是python的链式赋值...

  • OC 链式编程

    _ config.h _ View Example 既然已经决定,就勇敢的去吧。 决定吧

  • OC:链式编程

    概念: 链式编程:将多个业务逻辑(方法)通过“.”(点号)串联起来的一种代码风格,形似链条,故称链式编程。核心思想...

  • iOS链式语法深入实践

    要点 什么是链式语法 OC中的RAC、Masonry、SnapKit等链式编程的典型,大家应该都熟悉了Masonr...

  • PromiseKit框架(swift版)实践

    周所周知PromiseKit是个链式异步框架,用过 masnory的都知道链式异步的好处。该框架有OC和swift...

  • iOS 链式编程

    链式编程是OC中一种很好的设计模式。框架中使用链式编程,会让框架使用者感觉写的代码更加美观简洁。 链式编程的效果 ...

  • OC | 链式API学习

    实际开发中常见的链式API 使用masonry时经常会见到链式API,如 中的: 链式API由“点”链接而成,“点...

网友评论

      本文标题:OC: NSMutableAttributedString 链式

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