OC: NSMutableAttributedString 链式

2018-08-27  本文已影响0人  LiYaoPeng
屏幕快照 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地址

上一篇 下一篇

猜你喜欢

热点阅读