//利用此方法獲得換行後每行顯示的字數組
-(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
哎 寫的好沒邏輯 想了半天才作出來的 也是爲了本身之後能夠直接用 觀衆們看起來確定是一頭霧水的吧 哈哈哈 我只把關鍵方法添了上來 !!