YYText使用遇到的问题--文本无法点击

本文介绍使用YYKit库实现iOS应用中YYLabel组件的文本高亮显示及点击事件处理过程。通过实例演示了如何正确配置文本高亮范围,并触发相应的点击回调函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

突然想学学YYKit,一开始现将self.titleLb赋值了,然后再设置 yy_setTextHighlightRange ,发现点击了没有任何的反应,最后才发现,得先设置yy_setTextHighlightRange,然后再赋值给self.titleLb.




//错误代码:

#import "ViewController.h"

#import <YYText.h>




@interface ViewController ()


@property(nonatomic,strong)YYLabel * titleLb;

@end


@implementation ViewController


#pragma mark - lazy 



-(YYLabel*)titleLb

{

    if (!_titleLb) {

        _titleLb                         = [YYLabel new];

        _titleLb.frame                   = CGRectMake(10, 100, 300,40);

        _titleLb.userInteractionEnabled  = YES;

        _titleLb.numberOfLines           = 0;

        _titleLb.layer.borderWidth       = 1.0f;

        _titleLb.textVerticalAlignment   = YYTextVerticalAlignmentTop;

    }

    return _titleLb;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    NSMutableAttributedString  *text1 = [[NSMutableAttributedString alloc] initWithString:@"Some Text, blabla..."];

    

    // 2. Set attributes to text, you can use almost all CoreText attributes.

    text1.yy_font = [UIFont boldSystemFontOfSize:16];

    text1.yy_color = [UIColor blueColor];

    [text1 yy_setColor:[UIColor redColor] range:NSMakeRange(0, 4)];


    self.titleLb.attributedText = text1;



    [text1 yy_setTextHighlightRange:NSMakeRange(0, 4)//设置点击的位置

                             color:[UIColor redColor]

                   backgroundColor:[UIColor groupTableViewBackgroundColor]

                         tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)

    {

                             NSLog(@"这里是点击事件");

                         }];

    

    [self.view addSubview:self.titleLb];


}


//正确代码:

#import "ViewController.h"

#import <YYText.h>

@interface ViewController ()


@property(nonatomic,strong)YYLabel * titleLb;

@end


@implementation ViewController


#pragma mark - lazy 



-(YYLabel*)titleLb

{

    if (!_titleLb) {

        _titleLb                         = [YYLabel new];

        _titleLb.frame                   = CGRectMake(10100300,40);

        _titleLb.userInteractionEnabled  = YES;

        _titleLb.numberOfLines           = 0;

        _titleLb.layer.borderWidth       = 1.0f;

        _titleLb.textVerticalAlignment   = YYTextVerticalAlignmentTop;

    }

    return _titleLb;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    NSMutableAttributedString  *text1 = [[NSMutableAttributedString allocinitWithString:@"Some Text, blabla..."];

    

    // 2. Set attributes to text, you can use almost all CoreText attributes.

    text1.yy_font = [UIFont boldSystemFontOfSize:16];

    text1.yy_color = [UIColor blueColor];

    [text1 yy_setColor:[UIColor redColorrange:NSMakeRange(04)];


    [text1 yy_setTextHighlightRange:NSMakeRange(04)//设置点击的位置

                             color:[UIColor redColor]

                   backgroundColor:[UIColor groupTableViewBackgroundColor]

                         tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect)

    {

                             NSLog(@"这里是点击事件");

                         }];

    self.titleLb.attributedText = text1;

    [self.view addSubview:self.titleLb];


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值