```
NSString * showText = @"你的快递包裹到了,签收人:张三,电话:13987654321,送货员:李四,电话:15888888888,收件地址:火星";
UILabel*testTapLabel = [[UILabelalloc]initWithFrame:CGRectMake(0,100,self.view.frame.size.width,500)];
testTapLabel.numberOfLines=2;
testTapLabel.attributedText = [self getAttributeWith:@[@"13987654321",@"15888888888"] string:showText orginFont:12 orginColor:[UIColor darkGrayColor] attributeFont:12 attributeColor:[UIColor blueColor]]; [testTapLabelyb_addAttributeTapActionWithStrings:@[@"13987654321",@"15888888888"]tapClicked:^(UILabel*label,NSString*string,NSRangerange,NSIntegerindex) {
NSString* message = [NSStringstringWithFormat:@"点击了\"%@\"字符\nrange:%@\n在数组中是第%ld个",string,NSStringFromRange(range),index +1];
YBAlertShow(message,@"知道了");
}];
[self.viewaddSubview:testTapLabel];
}
- (NSAttributedString*)getAttributeWith:(id)sender
string:(NSString*)string
orginFont:(CGFloat)orginFont
orginColor:(UIColor*)orginColor
attributeFont:(CGFloat)attributeFont
attributeColor:(UIColor*)attributeColor
{
__block NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:string];
[totalStraddAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:orginFont] range:NSMakeRange(0, string.length)];
[totalStraddAttribute:NSForegroundColorAttributeNamevalue:orginColorrange:NSMakeRange(0, string.length)];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStylesetLineSpacing:5.0f];//设置行间距
[paragraphStylesetLineBreakMode:NSLineBreakByTruncatingTail];
[paragraphStylesetAlignment:NSTextAlignmentLeft];
[paragraphStylesetLineBreakMode:NSLineBreakByCharWrapping];
[totalStraddAttribute:NSParagraphStyleAttributeNamevalue:paragraphStylerange:NSMakeRange(0, [totalStrlength])];
if([senderisKindOfClass:[NSArrayclass]]) {
__blockNSString*oringinStr = string;
__weaktypeof(self) weakSelf =self;
[senderenumerateObjectsUsingBlock:^(NSString* _Nonnullstr,NSUIntegeridx,BOOL*_Nonnullstop) {
NSRangerange = [oringinStrrangeOfString:str];
[totalStraddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:attributeFont]range:range];
[totalStraddAttribute:NSForegroundColorAttributeNamevalue:attributeColorrange:range];
oringinStr = [oringinStrstringByReplacingCharactersInRange:rangewithString:[weakSelfgetStringWithRange:range]];
}];
}elseif([senderisKindOfClass:[NSStringclass]]) {
NSRangerange = [stringrangeOfString:sender];
[totalStraddAttribute:NSFontAttributeNamevalue:[UIFontsystemFontOfSize:attributeFont]range:range];
[totalStraddAttribute:NSForegroundColorAttributeNamevalue:attributeColorrange:range];
}
returntotalStr;
}
- (NSString*)getStringWithRange:(NSRange)range
{
NSMutableString *string = [NSMutableString string];
for(inti =0; i < range.length; i++) {
[stringappendString:@" "];
}
returnstring;
}
```