美文网首页
自动算文本高度oc

自动算文本高度oc

作者: VickyLanLan | 来源:发表于2018-04-27 17:42 被阅读63次

传入数组即可以算高度

+(CGFloat)calculationCellFrameWithArray:(NSArray*)array{

    CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离

    CGFloat h = 10;//用来控制button距离父视图的高

    for(inti =0; i < array.count; i++) {

        //根据计算文字的大小

        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};

        CGFloat length = [array[i] boundingRectWithSize:CGSizeMake(kScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;

        CGRectrect  =CGRectMake(10+ w, h, length +15,30);

        //当button的位置超出屏幕边缘时换行 kScreenWidth 只是button所在父视图的宽度

        if(10+ w + length +20>kScreenWidth){

            w =0;//换行时将w置为0

            h = h + rect.size.height+10;//距离父视图也变化

            rect =CGRectMake(10+ w, h, length +15,30);//重设button的frame

        }

        w = rect.size.width+ rect.origin.x;

    }    returnh +50;

}

相关文章

网友评论

      本文标题:自动算文本高度oc

      本文链接:https://www.haomeiwen.com/subject/vvrilftx.html