部分文字点击事件

转载原文
单独封装一个方法,根据传入文字的范围来设置点击的范围,使用block回调来实现点击事件

.h文件
#import <UIKit/UIKit.h>
//点击按钮
typedef void (^clickBlock)();
@interface PCBClickLabel : UIView
- (instancetype)initLabelViewWithLab:(NSString *)text clickTextRange:(NSRange)clickTextRange clickAtion:(clickBlock)clickAtion;
@property (nonatomic, copy)clickBlock clickBlock;
@end
.m文件
#define originY 60
#import "PCBClickLabel.h"
@interface PCBClickLabel()
@property (nonatomic, strong)UILabel *commonLabel;
@property (nonatomic, strong)UILabel *clickLabel;
@property (nonatomic, assign)CGRect commonLabelFrame;
@property (nonatomic, assign)CGRect clickLabelFrame;
@property (nonatomic, strong)NSString *clickLabStr;

@end

@implementation PCBClickLabel


- (instancetype)initLabelViewWithLab:(NSString *)text clickTextRange:(NSRange)clickTextRange clickAtion:(clickBlock)clickAtion{
    self = [super init];
    if (self) {
        //变量
        NSString *clickText;
        NSString *commonText;
        NSString *commonText2;
        UILabel *commonLabel = [UILabel new];
        UILabel *clickLabel = [UILabel new];
        UILabel *commonLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];

        //如果选择的范围文字是文尾的文字,而不是中间的文字或者开头的文字
        if (clickTextRange.location + clickTextRange.length == text.length) {
            clickText = [text substringWithRange:clickTextRange];
            commonText = [text substringToIndex:clickTextRange.location];
            //label的位置
            commonLabel.frame = CGRectMake(0, 0, [[self class] calculateRowWidth:commonText], 30);
            clickLabel.frame = CGRectMake(commonLabel.frame.size.width+commonLabel.frame.origin.x, 0, [[self class] calculateRowWidth:clickText], 30);
        }else if (clickTextRange.location == 0) {//选择的范围文字是开头的
            clickText = [text substringWithRange:clickTextRange];
            commonText = [text substringWithRange:NSMakeRange(clickText.length, text.length-clickText.length)];

            //label的位置
            clickLabel.frame = CGRectMake(0, 0, [[self class] calculateRowWidth:clickText], 30);
            commonLabel.frame = CGRectMake(clickLabel.frame.size.width+clickLabel.frame.origin.x, 0, [[self class] calculateRowWidth:commonText], 30);
        }else { //可点击文字在整段文字中间
            clickText = [text substringWithRange:clickTextRange];
            commonText = [text substringToIndex:clickTextRange.location];
            commonText2 = [text substringWithRange:NSMakeRange(clickTextRange.length+clickTextRange.location, text.length-clickText.length-commonText.length)];
            commonLabel2 = [UILabel new];
            //label的位置
            commonLabel.frame = CGRectMake(0, 0, [[self class] calculateRowWidth:commonText], 30);
            clickLabel.frame = CGRectMake(commonLabel.frame.size.width+commonLabel.frame.origin.x, 0, [[self class] calculateRowWidth:clickText], 30);
            commonLabel2.frame = CGRectMake(clickLabel.frame.size.width+clickLabel.frame.origin.x, 0, [[self class] calculateRowWidth:commonText2], 30);
            commonLabel2.text = commonText2;
            [self addSubview:commonLabel2];
        }

        commonLabel.text = commonText;
        clickLabel.text = clickText;
        clickLabel.textColor = [UIColor redColor];
        self.clickLabStr = clickText;
        clickLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labClick)];
        [clickLabel addGestureRecognizer:tap];


        self.backgroundColor = [UIColor yellowColor];
        self.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width-(clickLabel.frame.size.width+commonLabel.frame.size.width+commonLabel2.frame.size.width))/2, originY, [[self class] calculateRowWidth:text], 30);
        self.clickBlock = clickAtion;
//        self.clickLab = clickLabel;
        [self addSubview:commonLabel];
        [self addSubview:clickLabel];
    }
    return self;
}

+ (CGFloat)calculateRowWidth:(NSString *)string {
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};  //指定字号
    CGRect rect = [string boundingRectWithSize:CGSizeMake(0, 30)/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width;
}

- (void)labClick {
        NSLog(@"点击了”%@“",self.clickLabStr);
    self.clickBlock();
}
@end
封装完成后使用
PCBClickLabel *label = [[PCBClickLabel alloc]initLabelViewWithLab:@"我是水电费点击文字" clickTextRange:NSMakeRange(5, 2) clickAtion:^{
        //点击触发的事件
    }];
[self.view addSubview:label];

Paste_Image.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值