//利用此方法获得换行后每行显示的字数组
-(NSArray *)getSeparatedLinesFromLabel:(UILabel *)label框架
{函数
NSString *text = [label text];atom
UIFont *font = [label font];url
CGRect rect = [label frame];.net
CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);代理
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];orm
[attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];递归
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);事件
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
NSMutableArray *linesArray = [[NSMutableArray alloc]init];
for (id line in lines)
{
CTLineRef lineRef = (__bridge CTLineRef )line;
CFRange lineRange = CTLineGetStringRange(lineRef);
NSRange range = NSMakeRange(lineRange.location, lineRange.length);
NSString *lineString = [text substringWithRange:range];
[linesArray addObject:lineString];
}
return (NSArray *)linesArray;
}
//判断高度是否大于最大高度 大于默认显示最大高度 不然显示所有内容所占高度
for (int i = 0; i < arr.count; i ++) {//arr为内容数组 CircleModel为模型类
NSString *text = arr[i];
CircleModel *model = [[CircleModel alloc] init];
model.content = text;
model.index = i;
CGFloat height = [text boundingRectWithSize:CGSizeMake(KWidth-95, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
if (height <= 80) {
model.isMoreThanMaxHeight = NO;
model.contentHeight = height;
}else{
model.isMoreThanMaxHeight = YES;
model.isShow = NO;//大于高度默认收起
model.contentHeight = 80;
}
[_testArr addObject:model];
}
//cell中
//内容
if (!_model.isMoreThanMaxHeight) {//小于或等于默认最大高度
_contentLabel.text = _model.content;
_contentLabel.frame = SetFrame(_iconImageView.right+10, _iconImageView.bottom+10, KWidth-30-_iconImageView.right, _model.contentHeight);
}else{//大于默认高度
if (_model.isShow == NO) {
//收起状态 显示全文 点击全文显示所有内容 点击事件采用TTTAttributedLabel 框架 手动添加@“...全文”字符串于内容以后
_contentLabel.text = _model.content;
CGFloat rightHeight = [self getContentRightheight];//这是
_contentLabel.frame = SetFrame(_iconImageView.right+10, _iconImageView.bottom+10, KWidth-30-_iconImageView.right, rightHeight);
}else{
//展开状态 显示收起 点击收起显示最大高度能显示出的问题 手动添加@“收起”字符串于内容以后
这里就不写了 获得全部内容的高度并显示(包含收起)
}
#pragma mark 获得内容的正确高度 默认最大高度80
- (CGFloat)getContentRightheight{
CGFloat realHeight = [HHGolbalModel returnSieHeightWithLabel:_contentLabel withLabelFrame:_contentLabel.frame];
if (realHeight>contentMaxHeight) {
realHeight = contentMaxHeight;
NSString *text = @"";
CGFloat height = 0.0f;
NSArray *textArr = [self getSeparatedLinesFromLabel:_contentLabel];
for (int i = 0; i < textArr.count; i++) {
CGRect rect = [[textArr objectAtIndex:i] boundingRectWithSize:CGSizeMake(_contentLabel.width, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil];
height = height+rect.size.height;
if (height > contentMaxHeight) {
break;
}else{
text = [NSString stringWithFormat:@"%@%@",text,[textArr objectAtIndex:i]];
}
}
NSString *behindStr = @"...全文";
text = [NSString stringWithFormat:@"%@",GetText(text, behindStr, 1)];//这是一个递归函数
_contentLabel.text = text;
[_contentLabel setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
//设置可点击文字的范围
NSRange boldRange = NSMakeRange(text.length-2, 2);
//设定可点击文字的的大小
UIFont *boldSystemFont = [UIFont systemFontOfSize:16];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
//设置可点击文本的大小
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
//设置可点击文本的颜色
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[KGreenClor CGColor] range:boldRange];
CFRelease(font);
}
return mutableAttributedString;
}];
NSRange range = NSMakeRange(text.length-2, 2);
[_contentLabel addLinkToURL:nil withRange:range];
}
return realHeight;
}
//此递归函数用于返回内容高度大于默认最大高度时添加@"...全文"恰好显示正确的字符串
NSString * GetText(NSString *text,NSString *behind,NSInteger count){
NSString *str = [NSString stringWithFormat:@"%@%@",[text substringWithRange:NSMakeRange(0, text.length-count)],behind];
CGFloat height = [str boundingRectWithSize:CGSizeMake( KWidth-95, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
if (height < contentMaxHeight) {
return str;
}else{
return GetText([str substringWithRange:NSMakeRange(0, str.length-behind.length)], behind, 1);
}
}
#pragma mark 富文本代理 点击全文或收起时调用的代理
@protocol CellContentClick <NSObject>
- (void)contentIsShow:(BOOL)isShow withIndex:(NSInteger)index;
@end
- (void)attributedLabel:(__unused TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url
{
[self.delegate contentIsShow:_model.isShow withIndex:_model.index];
}
//控制器中实现cell代理
- (void)contentIsShow:(BOOL)isShow withIndex:(NSInteger)index{
CircleModel *model = [_testArr objectAtIndex:index];
model.isShow = !isShow;
if (isShow) {
//点击收起
model.contentHeight = 80;
}else{
//点击全文
CGFloat height = [[NSString stringWithFormat:@"%@ %@",model.content,@"收起"] boundingRectWithSize:CGSizeMake(KWidth-95, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} context:nil].size.height;
model.contentHeight = height;
}
[_testArr replaceObjectAtIndex:index withObject:model];
[_circleTableView reloadSections:[NSIndexSet indexSetWithIndex:index] withRowAnimation:UITableViewRowAnimationFade];
}
//model类
#import <Foundation/Foundation.h>
@interface CircleModel : NSObject
@property (nonatomic,strong) NSString *content;//圈子内容
@property (nonatomic,assign) BOOL isMoreThanMaxHeight;//内容高度是否大于最高高度
@property (nonatomic,assign) BOOL isShow;//当内容高度大于最高高度时判断是展开状态仍是收起状态
@property (nonatomic,assign) CGFloat contentHeight;//内容高度
@property (nonatomic,assign) NSInteger index;//indexPath.Row;
@end
哎 写的好没逻辑 想了半天才作出来的 也是为了本身之后能够直接用 观众们看起来确定是一头雾水的吧 哈哈哈 我只把关键方法添了上来 !!