iOS-YYLabel实现部分文字的点击事件

2019-05-22  本文已影响0人  fly大梦想家
描述:用label实现部分文字点击事件,并设置内边距
yylabel部分文字的点击事件.gif
    _topPromptLabel = [YYLabel new];
    _topPromptLabel.font = FONT_SIZE_REGULAR_SCALE(22, YES);
    _topPromptLabel.backgroundColor = [GOVCommon colorForKey:@"conferenceBottomBgColor"];
    _topPromptLabel.textContainerInset = UIEdgeInsetsMake(0, LAYOUT_SIZESCALE_PORTRAIT(24,24).width, 0, 0);
NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithString:promptStr];
    mutableString.color = [GOVCommon colorForKey:@"conferenceMsgPromptColor"];
    [mutableString setTextHighlightRange:NSMakeRange(promptStr.length - 4, 4) color:[UIColor whiteColor] backgroundColor:[UIColor redColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSLog(@"点击认证被点击了");
    }];
    self.topPromptLabel.attributedText = mutableString;
    self.topPromptLabel.frame = CGRectMake(0, _contentH + LAYOUT_SIZESCALE_PORTRAIT(173, 173).height, LAYOUT_SCREENSIZE_P.width, LAYOUT_SIZESCALE_PORTRAIT(34, 34).height);
 [_scrollView addSubview:_topPromptLabel];

如果还想给文本添加颜色用之前的方法不行

[mutableString addAttribute:NSForegroundColorAttributeName value:[GOVCommon colorForKey:@"darkGrayTextColor"] range:NSMakeRange(0, 3)];

需要再写一遍

[mutableString setTextHighlightRange:NSMakeRange(0, 3) color:[GOVCommon colorForKey:@"darkGrayTextColor"] backgroundColor:[UIColor clearColor] userInfo:nil];
上一篇 下一篇

猜你喜欢

热点阅读