iOS YYText展开文字

#import "BMKCNCommonWordsCell.h"
#import "YYText.h"
@interface BMKCNCommonWordsCell()
@property (strong ,nonatomic) YYLabel *mytextLabel;
@property (nullable, nonatomic, copy) NSAttributedString *truncationToken;
@end

@implementation BMKCNCommonWordsCell

-(void)setItem:(BMKCNCommonWordsListModel *)item{
    _item = item;
    self.mytextLabel.text = item.content;
    
    if (item.is_showAll) {
        self.mytextLabel.numberOfLines = 0;
    }else{
        self.mytextLabel.numberOfLines = 1;
    }
}
- (void)rj_setTableViewCell{
    [super rj_setTableViewCell];
    // 创建并添加 YYLabel ...
    self.mytextLabel = [[YYLabel alloc] init];
    NSString *string = @"";
    self.mytextLabel.text = string;
    self.mytextLabel.font = rj_kFont_26;
    self.mytextLabel.textColor = rj_kColor_333333;
    self.mytextLabel.preferredMaxLayoutWidth = rj_kScreenWidth - 30;
    self.mytextLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    [self.contentView addSubview:self.mytextLabel];
    [self.mytextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.bottom.equalTo(self.contentView).offset(-15);
    }];

    // 创建属性字符串"...展开"
    NSMutableAttributedString *expandString = [[NSMutableAttributedString alloc] initWithString:@"... 展开"];
    expandString.yy_font = self.mytextLabel.font;
    NSRange highlightRange = [expandString.string rangeOfString:@"展开"];
    [expandString yy_setColor:k_ypb_mainColor range:highlightRange];

    // 创建一个“高亮”属性
    YYTextHighlight *highlight = [YYTextHighlight new];
    [highlight setFont:self.mytextLabel.font];
    [highlight setColor:k_ypb_mainColor];
        
    // !!!: 点击「展开」按钮,执行的动作
    rj_weakify(self)
    highlight.tapAction = ^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        // TODO: 点击展开按钮后,完整显示所有内容
        [self.mytextLabel sizeToFit];
        if (RJWeak_self.pleaseUpdate) {
            RJWeak_self.pleaseUpdate();
        }
    };
    
    [expandString yy_setTextHighlight:highlight range:highlightRange];
    
    // 将属性字符串设置到 YYLabel 中
    YYLabel *expandLabel = [[YYLabel alloc] init];
    expandLabel.attributedText = expandString;
    [expandLabel sizeToFit];
    
    // 将此带高亮属性的 YYLabel 设置为整个 YYLabel 的截断符
    NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:expandLabel
                                                                                 contentMode:UIViewContentModeCenter
                                                                              attachmentSize:expandLabel.grj_size
                                                                                 alignToFont:expandString.yy_font
                                                                                   alignment:YYTextVerticalAlignmentCenter];
    self.mytextLabel.truncationToken = truncationToken;

}
@end

你可能感兴趣的:(iOS YYText展开文字)