YBAttributeTapAction

```

? ? 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:@"點(diǎn)擊了\"%@\"字符\nrange:%@\n在數(shù)組中是第%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];//設(shè)置行間距

? ? [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;

}

```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容