Label上的富文本及图文混排,YYLabel部分文字可点击及换行失效问题

1.类似于如图:
6781590-386644a99a5e1971.png
image.png
1.NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] 
initWithString:@"文字(必填)" attributes:@{NSFontAttributeName : Mfont(12), 
NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#FFC1B3"]}];

NSMutableAttributedString *attributedString = @"(必填)";
NSRange range = NSMakeRange(2, attributedString.length);
[attri addAttribute:NSFontAttributeName value:KBlodfont(15) range:range];

2.NSTextAttachment *attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"pic.png"];
attach.bounds = CGRectMak(0,0,14,14);

3.NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attch];
[attri insertAttributedString:string atIndex:1];
label.attributedText = attri;
2.YYLabel部分文字可点击及换行失效问题:比如可以点击小组名字进入小组,新的社群***等文字比较长没有换行自适应
6781590-e2a16551a859fded.png
image.png
 self.noticeLabel = [[YYLabel alloc] init];
 self.noticeLabel.textColor = REXADECIMALCOLOR(@"212121");
 self.noticeLabel.font = KBlodfont(16);
 self.noticeLabel.numberOfLines = 0;//需要允许换行
 self.noticeLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - ScreenX375(112);//需要设置一个最佳的最大布局宽度.
 self.noticeLabel.userInteractionEnabled = YES;
 [self.noticeLabel sizeToFit];

  NSRange range = NSMakeRange([[text string] rangeOfString:keyWord].location, keyWord.length);
  //text所有的内容 keyWord需要高亮的部分文字

  [text setTextHighlightRange:range color:UIColorFromRGB(@"#00A4FF", 1) backgroundColor:kBDCellTextHighlightBackgroundColor userInfo:@{} tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        //进行点击后的操作

  } longPressAction:nil];
  self.noticeLabel.attributedText = text;

转载于:https://www.jianshu.com/p/aeaa7c36581d

猜你喜欢

转载自blog.csdn.net/weixin_33698823/article/details/91164745
今日推荐