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:@"点击了\"%@\"字符\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;

}

```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容