【版本适配】iOS中文本的自适应高度

//文字高度

+ (float)height:(NSString *)lab

{

#ifdef __IPHONE_7_0

    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize: K_FONT_SIZE],NSFontAttributeName, nil];

    

    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {

        CGRect rect =  [lab boundingRectWithSize:CGSizeMake(K_INTRODUCE_LABLE_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

        if ([lab isEqualToString:@""]) {

            rect.size.height = 0;

        }

        return rect.size.height;

    }

    else

    {

        CGSize rect = [lab sizeWithFont:[UIFont systemFontOfSize:K_FONT_SIZE] constrainedToSize:CGSizeMake(220,2000) lineBreakMode:NSLineBreakByTruncatingHead];

        if ([lab isEqualToString:@""]) {

            rect.height = 0;

        }

        return rect.height;

    }

#endif

}

- (void)setCellVal:(NSString *)cellVal

{

    if (_cellVal != cellVal) {

        [_cellVal release];

        _cellVal = [cellVal retain];

    }

    self.cellLabel.text = cellVal;


#ifdef __IPHONE_7_0

    //计算文本在限定范围内的高度

    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:K_FONT_SIZE], NSFontAttributeName, nil];

    

    if ([[[UIDevice currentDevice]systemVersion]floatValue]>=7.0) {

        CGRect rect =  [_cellLabel.text boundingRectWithSize:CGSizeMake(K_INTRODUCE_LABLE_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

        _cellLabel.frame = CGRectMake(_cellLabel.left, _cellLabel.top, _cellLabel.width, rect.size.height);

    }

    else if ([[[UIDevice currentDevice]systemVersion]floatValue] < 7.0){

        

        CGSize rect = [_cellLabel.text sizeWithFont:[UIFont systemFontOfSize:K_FONT_SIZE] constrainedToSize:CGSizeMake(220,2000) lineBreakMode:NSLineBreakByTruncatingHead];

        _cellLabel.frame = CGRectMake(_cellLabel.left, _cellLabel.top, _cellLabel.width, rect.height);

    }

#endif

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值