第一種方法
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat WIDTH = [UIScreen mainScreen].bounds.size.width;
NSString *str = @"Some Text, blabla...哈哈啊哈哈哈哈呵《我是協(xié)議》呵《我是協(xié)議》額呵呵呵呵好的好的好的好的好的好的好的";
// 1. 創(chuàng)建一個"高亮"屬性,當(dāng)用戶點擊了高亮區(qū)域的文本時警检,"高亮"屬性會替換掉原本的屬性
YYTextBorder *border = [YYTextBorder borderWithFillColor:[UIColor yellowColor] cornerRadius:3];
YYTextHighlight *highlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor redColor]];
[highlight setColor:[UIColor greenColor]];
[highlight setBackgroundBorder:border];
// highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
// NSLog(@"tap text range:...");
// // 你也可以把事件回調(diào)放到 YYLabel 和 YYTextView 來處理甜害。
// };
// 1. 創(chuàng)建一個屬性文本
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
text.yy_font = [UIFont boldSystemFontOfSize:30];
text.yy_color = [UIColor blueColor];
text.yy_lineSpacing = 10;
NSString *highlightStr = @"《我是協(xié)議》";
NSArray *array = [self rangeOfSubString:highlightStr inString:str];
for (NSInteger i = 0; i < array.count; i++) {
NSValue *value = array[i];
// 2. 把"高亮"屬性設(shè)置到某個文本范圍
[text yy_setTextHighlight:highlight range:value.rangeValue];
[text yy_setColor:[UIColor redColor] range:value.rangeValue];
}
// 3. 賦值到 YYLabel 或 YYTextView
YYLabel *label = [[YYLabel alloc] init];
label.numberOfLines = 0;
label.preferredMaxLayoutWidth = WIDTH-30;
label.attributedText = text;
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(15);
make.right.mas_equalTo(-15);
make.top.mas_equalTo(100);
}];
label.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSLog(@"textTapAction");
};
}
- 注意:YYText用masonry布局要想自適應(yīng)高度就必須設(shè)置以下兩點
label.numberOfLines = 0;
label.preferredMaxLayoutWidth = WIDTH-30;
第二種方法
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat WIDTH = [UIScreen mainScreen].bounds.size.width;
NSString *str = @"Some Text, blabla...哈哈啊哈哈哈哈呵《我是協(xié)議》呵《我是協(xié)議》額呵呵呵呵好的好的好的好的好的好的好的";
// 1. 創(chuàng)建一個屬性文本
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
NSString *highlightStr = @"《我是協(xié)議》";
NSArray *array = [self rangeOfSubString:highlightStr inString:str];
// 2. 為文本設(shè)置屬性
text.yy_font = [UIFont boldSystemFontOfSize:30];
text.yy_color = [UIColor blueColor];
text.yy_lineSpacing = 10;
for (NSInteger i = 0; i < array.count; i++) {
NSValue *value = array[i];
// [text yy_setColor:[UIColor redColor] range:value.rangeValue];
[text yy_setTextHighlightRange:value.rangeValue
color:[UIColor redColor]
backgroundColor:[UIColor grayColor]
tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
NSLog(@"tap text range:...");
}];
}
// 3. 賦值到 YYLabel 或 YYTextView
YYLabel *label = [[YYLabel alloc] init];
label.numberOfLines = 0;
label.preferredMaxLayoutWidth = WIDTH-30;
label.attributedText = text;
[self.view addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(15);
make.right.mas_equalTo(-15);
make.top.mas_equalTo(100);
}];
// label.textTapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
// NSLog(@"點擊了");
// };
}
- 獲取一個字符在字符串中出現(xiàn)的所有位置 返回一個被NSValue包裝的NSRange數(shù)組
//獲取一個字符在字符串中出現(xiàn)的所有位置 返回一個被NSValue包裝的NSRange數(shù)組
- (NSArray *)rangeOfSubString:(NSString *)subStr inString:(NSString *)string {
if (subStr == nil && [subStr isEqualToString:@""]) {
return nil;
}
NSMutableArray *rangeArray = [NSMutableArray array];
NSString *string1 = [string stringByAppendingString:subStr];
NSString *temp;
for (int i = 0; i < string.length; i ++) {
temp = [string1 substringWithRange:NSMakeRange(i, subStr.length)];
if ([temp isEqualToString:subStr]) {
NSRange range = {i,subStr.length};
[rangeArray addObject:[NSValue valueWithRange:range]];
}
}
return rangeArray;
}