iOS tableview中UILabel点击全文展开 点击收起

//利用此方法得到换行后每行显示的字

-(NSArray *)getSeparatedLinesFromLabel:(UILabel *)label

{

    NSString *text = [label text];

    UIFont   *font = [label font];

    CGRect    rect = [label frame];

    

    CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);

    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];

    [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

 

 

哎  写的好没逻辑  想了半天才做出来的  也是为了自己以后可以直接用  观众们看起来肯定是一头雾水的吧  哈哈哈  我只把关键方法添了上来 !!

 

转载于:https://my.oschina.net/u/2488236/blog/806199

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值