NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc]initWithString:@"使用范围来进行标记的字符串,可以在字符串在添加图片进行点击,实现再字符串中插入按钮的效果,有颜色的都可点击"];
NSRange rang1 = [attributedString2.string rangeOfString:@"标记"];
NSRange rang2 = [attributedString2.string rangeOfString:@"点击"];
NSRange rang3 = [attributedString2.string rangeOfString:@"按钮"];
[attributedString2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, attributedString2.length)];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:rang1];
[attributedString2 addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:rang2];
NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"abc.jpg"];
attach.bounds = CGRectMake(0, 0, 15, 15);
NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
[attributedString2 insertAttributedString:attachString atIndex:rang3.location];
_textLabel2.attributedText = attributedString2;
[_textLabel2 addAttributeActionWithRange:[NSArray arrayWithObjects:[NSValue valueWithRange:rang1],[NSValue valueWithRange:rang2],[NSValue valueWithRange:rang3],nil] tapTargetAction:^(NSString *string, NSRange range, NSInteger index) {
NSLog(@"点击字符串:%@ 范围在%@,第%ld个",string,NSStringFromRange(range),index+1);
}];