ios实现文字的自适应

如果你是用xib搭的cell界面   那么cell上面的UIlabel就不能设置宽高   要选择上下左右自适应   并且label的行数设置为0  然后在tableView的代理方法

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

写如下代码

cell.label.text = @"..................................等等一堆字";

        CGSize textSize1 = [model.content boundingRectWithSize:tableView.bounds.size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingTruncatesLastVisibleLine  attributes:nil context:nil].size;       

NSLog(@"%f",textSize1.height);

        // cell.label.frame.size.height -----------------> 是错误的,大家可以思考一下为什么是错误的

        CGRect rect = cell.lbContent.frame;

        rect.size.height = textSize1.height+20;//是为了给最后一行字展示留出多点位置

        cell.lbContent.frame = rect;

        self.tableView.rowHeight = rect.size.height + 80;  //是为了上下留出点距离自己设置

原文地址:https://www.cnblogs.com/sunfuyou/p/5945694.html