美文网首页
iOS-YYLabel实现部分文字的点击事件

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

作者: fly大梦想家 | 来源:发表于2019-05-22 15:46 被阅读0次
    描述:用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];
    

    相关文章

      网友评论

          本文标题:iOS-YYLabel实现部分文字的点击事件

          本文链接:https://www.haomeiwen.com/subject/iqduzqtx.html