YYLabel富文本

富文本

920 发布: 2024/9/11 17:36 本文总阅读量

富文本显示

  • yylabel中不能使用NSTextAttachment来处理富文本

正确方案

UIImageView *imageV = [[UIImageView alloc] init];
imageV.frame = CGRectMake(0, -2, 15, 15);
imageV.image = emojiImage;
NSAttributedString *imageString = [NSAttributedString yy_attachmentStringWithContent:imageV contentMode:UIViewContentModeScaleAspectFit attachmentSize:CGSizeMake(15, 15) alignToFont:_contentL.font alignment:YYTextVerticalAlignmentCenter];

_contentL.attributedText = imageString;

或者

UIImage *image = [UIImage imageNamed:@"壁纸-详情-编辑"];
NSMutableAttributedString *attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(15, 15) alignToFont:SYS_Font(15) alignment:(YYTextVerticalAlignment)YYTextVerticalAlignmentCenter];

连接

展开更多


/// 调用
[self addSeeMoreWithYYLabel:_playValL andText:playStr];

/// 具体实现
// yylable
-(YYLabel *)createYYlable {
    YYLabel *yyL = [[YYLabel alloc]init];
    yyL.font = SYS_Font(14);
    yyL.textColor = RGB_COLOR(@"#888888", 1);
    yyL.preferredMaxLayoutWidth = _window_width-32;
    yyL.numberOfLines = 3;
    // yyL.userInteractionEnabled = YES;
    return yyL;
}
// 内容
- (void)addSeeMoreWithYYLabel:(YYLabel *)yylabel andText:(NSString *)showStr{
    // 基础文本
    UIFont *textFont =  [UIFont systemFontOfSize:14];
    NSMutableAttributedString *showAtt = [[NSMutableAttributedString alloc]initWithString:showStr];
    NSRange showRange = [showAtt yy_rangeOfAll];
    [showAtt addAttribute:NSForegroundColorAttributeName value:RGB_COLOR(@"#888888", 1) range:showRange];
    showAtt.yy_font = textFont;
    yylabel.attributedText = showAtt;
    
    // 展开更多
    UIFont *moreFont = [UIFont boldSystemFontOfSize:14];
    NSMutableAttributedString *openColAttStr = [[NSMutableAttributedString alloc] initWithString:YZMsg(@"展开更多")];
    [openColAttStr addAttribute:NSFontAttributeName value:moreFont range:[openColAttStr yy_rangeOfAll]];
    UIImage *down_img = [UIImage imageNamed:@"creator_down"];
    NSMutableAttributedString *imgAttStr = [NSMutableAttributedString yy_attachmentStringWithContent:down_img contentMode:UIViewContentModeCenter attachmentSize:CGSizeMake(12, 12) alignToFont:moreFont alignment:YYTextVerticalAlignmentCenter];
    [openColAttStr appendAttributedString:imgAttStr];
    // 带点
    NSMutableAttributedString *moreAttStr = [[NSMutableAttributedString alloc] initWithString:@"..."];
    [moreAttStr appendAttributedString:openColAttStr];
    NSRange click_range = [[moreAttStr string] rangeOfString:[openColAttStr string]];
    YBWeakSelf;
    [moreAttStr yy_setTextHighlightRange:click_range color:RGB_COLOR(@"#333333", 1) backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        // 点击全文回调
        [weakSelf clickHighlightRange:yylabel];
    }];
    moreAttStr.yy_font = moreFont;
    
    YYLabel *seeMore = [YYLabel new];
    seeMore.attributedText = moreAttStr;
    [seeMore sizeToFit];
    NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:moreFont alignment:YYTextVerticalAlignmentCenter];
    yylabel.truncationToken = truncationToken;
}
// 点击事件
-(void)clickHighlightRange:(YYLabel *)yylabel {
    YYLabel *label = yylabel;
    label.numberOfLines = 0;
    [label sizeToFit];
    
    // contentSize
    [_contentView layoutIfNeeded];
    CGFloat contentH = CGRectGetHeight(_contentView.frame) + 16;
    _scrollView.contentSize = CGSizeMake(0, contentH);
}