MZSelectableLabel使用

谷梁弘深
2023-12-01

使用文本超链接使用到MZSelectableLabel,记录使用步骤:

 MZSelectableLabel * label = [[MZSelectableLabel alloc]initWithFrame:CGRectMake(15, 40,self.view.frame.size.width-30,self.view.frame.size.width/320*45)];

    label.numberOfLines = 0;
    [label setFont:[UIFont fontWithName:@"Arial" size:14]];

        NSMutableAttributedString * str=[[NSMutableAttributedString alloc]initWithString:@"这是红色,这是绿色!"];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(2,2)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(7,2)];

        label.attributedText=str;

        NSMutableArray *array=[[NSMutableArray alloc]init];
        MZSelectableLabelRange *range1=[MZSelectableLabelRange selectableRangeWithRange:NSMakeRange(2,2) color:[UIColor clearColor]];
        range1.isArray=YES;
        MZSelectableLabelRange *range2=[MZSelectableLabelRange selectableRangeWithRange:NSMakeRange(7,2) color:[UIColor clearColor]];
        range2.isArray=YES;
        [array addObject: range1];
        [array addObject:range2];
        [label setSelectableRanges:array];

        label.selectionHandler = ^(NSRange range, NSString *string){
            if (range.location==2) {
                NSLog(@"red");
            }
            if (range.location==7) {
                NSLog(@"green");
            }

        };
    [self.view addSubview:label];
 类似资料: