項(xiàng)目需要實(shí)現(xiàn)文本過(guò)多的時(shí)候最多展示五行,其余的折疊。點(diǎn)擊可展開(kāi),再次點(diǎn)擊可收起。
效果如下:
這是在UITableView的Cell里實(shí)現(xiàn)的,需要自適應(yīng)高度。
一開(kāi)始自定義了一個(gè)UIView直接追加到需要添加展開(kāi)的最后一行的最后位置,覆蓋了展示的內(nèi)容。實(shí)現(xiàn)的并不完美。效果如下:
UI提出了改進(jìn):展開(kāi)和收起都需要更在文本末尾。
網(wǎng)上搜索了一下下,找到了適合自己的方法:鏈接? ? ?
下面記錄一下,加強(qiáng)記憶。
我這里是借用的xib來(lái)實(shí)現(xiàn)的布局,所以這段文本的高度單獨(dú)拿出來(lái)了
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *detailHeight;
獲取五行文本的高度
self.attDic = [NSString attDicWithFont:[UIFont systemFontOfSize:[NSString iPhoneFont:T_2 iPadFont:T_2 + 4]]
textColor: [UIColor useLight:HexColor(0x616A73) Dark:HexAColor(DarkTitle,.6)]
textSpacing:0
lineSpacing:5
lineHeightMultiple:0
firstLineHeadIndent:0
alignment:(NSTextAlignmentJustified)
headIndent:0
tailIndent:0
paragraphSpacingBefore:0
paragraphSpacing:0];
self.heightOfFiveLines = [NSString getHeightOfAttributedString:@"五\n行\(zhòng)n文\n字\n字" width:200 attibuteDic:self.attDic];
//attDic是自己封裝的包含文本屬性的字典
//文本顯示的處理
- (void)setTextForDetail:(NSString *)introInfos {
if (![introInfos isEqualToString:@""]) {
self.label_detail.hidden = NO;
self.background_detail.hidden = NO;
self.label_detail.attributedText = [[NSAttributedString alloc]initWithString:introInfos attributes:self.attDic];
self.heightsOfTexts = [NSString getHeightOfAttributedString:introInfos width:SCREEN_WIDTH-30*2 attibuteDic:self.attDic];//文本高度
if (self.heightsOfTexts <= self.heightOfFiveLines) {//不需要折疊,不需要添加展開(kāi)
self.label_detail.userInteractionEnabled = NO;
self.label_detail.numberOfLines = 0;
self.detailHeight.constant = self.heightsOfTexts + 12*2;
self.topMargin.constant = 10;
}else{
self.label_detail.userInteractionEnabled = YES;
self.view_fold.hidden = NO;
if (self.foldStatus) {//折疊態(tài)
self.label_detail.numberOfLines = 5;
self.detailHeight.constant = self.heightOfFiveLines + 12*2;
[self setAdjustableTextWithDesc:introInfos];
}else{
self.heightsOfTexts = [NSString getHeightOfAttributedString:[NSString stringWithFormat:@"%@...收起",introInfos] width:SCREEN_WIDTH-30*2 attibuteDic:self.attDic];
self.label_detail.numberOfLines = 0;
self.detailHeight.constant = self.heightsOfTexts + 12*2;
[self setShowTextWithDesc:introInfos];
}
self.topMargin.constant = 10;
}
}else{
self.label_detail.hidden = YES;
self.label_detail.text = @"";
self.background_detail.hidden = YES;
self.detailHeight.constant = 0;
self.topMargin.constant = -20;
}
}
//追加“...收起”
- (void)setShowTextWithDesc:(NSString *)desc {
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@...收起",desc] attributes:self.attDic];
@weakify(self)
//設(shè)置高亮色和點(diǎn)擊事件
[text yy_setTextHighlightRange:[[text string] rangeOfString:@"收起"]
color:HexColor(0x29CCCC)
backgroundColor:[UIColor clearColor]
tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
@strongify(self)
[self action_fold];
}];
self.label_detail.attributedText = text;
}
//追加“...展開(kāi)”
- (void)setAdjustableTextWithDesc:(NSString *)desc {
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:desc attributes:self.attDic];
self.label_detail.attributedText = text;
NSMutableAttributedString *showAll = [[NSMutableAttributedString alloc] initWithString:@"...展開(kāi)" attributes:self.attDic];
@weakify(self)
[showAll yy_setTextHighlightRange:[[showAll string] rangeOfString:@"展開(kāi)"]
color:HexColor(0x29CCCC)
backgroundColor:[UIColor clearColor]
tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
@strongify(self)
[self action_fold];
}];
//注釋掉的地方是一開(kāi)始用的添加附件這樣的方式寫(xiě)的,其實(shí)在這個(gè)場(chǎng)景中并不需要,直接追加富文本就可
//YYLabel *seeMore = [YYLabel new];
//seeMore.attributedText = showAll;
//seeMore.backgroundColor = [UIColor useLight:HexColor(0xFAFBFC) Dark:HexColor(DarkBackground)];
//[seeMore sizeToFit];
//NSMutableAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeScaleToFill attachmentSize:seeMore.size alignToFont:[UIFont systemFontOfSize:[NSString iPhoneFont:T_2 iPadFont:T_2 + 4]] alignment:YYTextVerticalAlignmentTop];
//self.label_detail.truncationToken = truncationToken;
self.label_details.truncationToken = showAll;
}
點(diǎn)擊事件是個(gè)block,回傳給UITableView
- (void)action_fold{
if (self.block_fold) {
self.block_fold([NSNumber numberWithBool:self.foldStatus]);
}
}
UITableView的代理方法:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath的處理文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-594608.html
BookSheetDetailListBCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BookSheetDetailListBCell"];
cell.foldStatus = [self.foldStatusArray[indexPath.row] boolValue];
cell.infoDic = self.dataArray[indexPath.row];
cell.block_fold = ^(id _Nonnull info) {
@strongify(self)
self.foldStatusArray[indexPath.row] = @(![info boolValue]);
[self.tableView reloadRowAtIndexPath:indexPath withRowAnimation:(UITableViewRowAnimationFade)];
};
return cell;
默認(rèn)是不展開(kāi)的狀態(tài)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-594608.html
到了這里,關(guān)于iOS 結(jié)合YYLabel實(shí)現(xiàn)文本的展開(kāi)和收起的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!