#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展开文字
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 前言 前段时间有朋友问我如何做类似展开显示全文的功能,那时候没空也没研究,这几天在群里发现也有朋友需要,于是我也就...
- 最近有点空闲时间,想着文字加图片高度自适应用的比较多,于是就弄了个demo,样式可根据需求自行修改,这里就不放代码...
- 操作 先github下载<YYText>文件导入, 代码如下: 来自:https://www.cnblogs.co...
- iOS用户协议界面实现方案(YYText实现部分文字的高亮点击) 通常情况下注册、登录界面下方都会包含用户协议和隐...
- 一谈《道德与法治》课上的有效化活动教学 道德教育要回归生活,怎样让《道德与法治》教学提高实效性,达到育人目的...