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

突然想学学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];


}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Bison中,yytext是一个全局变量,用于存放当前解析的token的文本内容。通常情况下,我们可以在Bison的语法规则中通过使用$1、$2等符号来引用yytext,这些符号代表了当前解析的token的文本内容。 例如,假设我们需要解析一个简单的表达式,表达式中只包含加、减、乘、除四种运算符和数字。那么我们可以定义如下的Bison语法规则: ``` %{ #include <stdio.h> %} %token NUMBER %token PLUS %token MINUS %token TIMES %token DIVIDE %% expr: NUMBER | expr PLUS expr | expr MINUS expr | expr TIMES expr | expr DIVIDE expr ; %% int main() { yyparse(); return 0; } int yyerror(const char *msg) { fprintf(stderr, "Error: %s\n", msg); return 0; } int yylex() { int c = getchar(); if (isdigit(c)) { ungetc(c, stdin); scanf("%d", &yylval); return NUMBER; } else if (c == '+') { return PLUS; } else if (c == '-') { return MINUS; } else if (c == '*') { return TIMES; } else if (c == '/') { return DIVIDE; } else if (c == '\n' || c == EOF) { return 0; } else { return -1; } } ``` 在上述代码中,我们定义了一个简单的表达式语法规则,其中每个token都有一个相应的词法分析器函数yylex()来生成。同时,我们还定义了一个yyerror()函数,用于处理语法错误。当Bison解析表达式时,它会自动将当前解析的token的文本内容存放到yytext中,并将其传递到语法规则中。 例如,当我们输入一个简单的表达式"1+2"时,Bison会先调用yylex()函数来获取第一个token,也就是数字"1",然后将其存放到yylval中,并返回NUMBER这个token类型。接下来,Bison会继续调用yylex()函数来获取下一个token,也就是加号"+",然后将其存放到yytext中,并返回PLUS这个token类型。最后,Bison会根据语法规则来解析表达式,并输出结果"3"。 可以看到,在Bison中使用yytext非常简单,我们只需要在语法规则中使用相应的符号$1、$2等来引用yytext即可。如果需要在Bison的代码中直接访问yytext,则可以直接使用全局变量yytext来获取其值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值