描述:用label实现部分文字点击事件,并设置内边距

_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];
网友评论