iOS label自适应高度

来源:互联网 发布:淘宝上专卖店有假货吗 编辑:程序博客网 时间:2024/04/30 03:20

ios7 cell 获取高度时  如果用以前的方法 会有警告 

那么如果 根据文字大小获取cell的高度呢  ? 

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        

        NSString *str = [_dataArray objectAtIndex:indexPath.row];

        UIFont *tfont = [UIFont systemFontOfSize:14.0];

        NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];

        ////////   iOS 7

        CGSize sizeText = [str boundingRectWithSize:CGSizeMake(3201000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

        /////////ios 6

        CGSize sizeText1 = [str sizeWithFont:[UIFont systemFontOfSize:16.0f] constrainedToSize:CGSizeMake(3201000) lineBreakMode:NSLineBreakByCharWrapping];

        return sizeText1.height+50; 

        

    }


0 0