链式调用
思路
- 定义一些相应的返回值为
NSMutableAttributedString(^)(id value)
的方法
例子
注意这里的实现,需要用weakSelf 若引用
- 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;
}
- 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属性操作
- 根据range 提取出
subString
,并mutableCopy
- 闭包提供接口,外面来修改
subString
- 把设置好的
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];
});
}];
查询特定字符
- 定义
model
:AttributedStrFiltrateRuler
,用来储存相应的查询规则与查询结果
/// 正则表达式
@property (nonatomic,copy) NSString *expressionString;
/// 所有的符合条件的 range集合
@property (nonatomic,strong) NSMutableArray <NSValue *>*resultRangeArray;
/// NSTextCheckingResult
@property (nonatomic,strong) NSArray <NSTextCheckingResult *>*textCheckingResultArray;
@property (nonatomic,strong) UIColor *color;
- 创建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;
}
网友评论