借助YYText很好实现的
NSMutableAttributedString *yytext = [[NSMutableAttributedString alloc]initWithString:model.message];
yytext.yy_font = 15;
yytext.yy_lineSpacing=5;
yytext.yy_color = [UIColor grayColor];
_yylab.numberOfLines = 2;
_yylab.attributedText = yytext;
[_yylab.attributedText addSeeMoreButton:_yylab more:@"全文" moreColor:JHMaincolor before:@"..." tapAction:nil];
NSAttributedString的扩展 #import "NSAttributedString+utils.h"
- (NSAttributedString*)addSeeMoreButton:(YYLabel*)label more:(NSString*)more moreColor:(UIColor*)morecolor before:(NSString*)before tapAction:(void(^)(UIView*containerView,NSAttributedString*text,NSRangerange,CGRectrect))tapAction{
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",before? before:@"",more ? more :@""]];
if(tapAction) {
YYTextHighlight *hi = [YYTextHighlight new];
[hi setColor:self.yy_color?self.yy_color:label.textColor];
hi.tapAction= tapAction;
[text yy_setTextHighlight:hirange:[text.stringrangeOfString:more]];
}
[text yy_setColor: morecolor ? morecolor: [UIColorcolorWithRed:0.000green:0.449blue:1.000alpha:1.000]range:[text.stringrangeOfString:more]];
text.yy_font =self.yy_font ? self.yy_font :label.font;
YYLabel*seeMore = [[YYLabelalloc]init];
seeMore.attributedText= text;
[seeMoresizeToFit];
NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.frame.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter];
label.truncationToken= truncationToken;
return truncationToken;
}
最终效果:
效果图