如何根据String调整UILabel宽度 [英] How to adjust the UILabel width according to the String

查看:30
本文介绍了如何根据String调整UILabel宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView,里面装满了自定义单元格.自定义单元格有许多标签.一个标签从数据库中获取不同大小的字符串.我想要的是下一个标签在第一个标签之后(在第一个标签字符串的末尾)开始.我使用了这个代码:

I have a UITableView, filled with custom cells. The custom cell has many labels. One label it takes different size strings from the db. What I want is for the next label to start right after the first label (at the end of the string of first lable). I used this code:

cell.Manufactlbl.adjustsFontSizeToFitWidth=YES;
CGSize lLabelSIze = [coffeeObj.price sizeWithFont: cell.Manufactlbl.fontforWidth:cell.Manufactlbl.frame.size.width lineBreakMode:cell.Manufactlbl.lineBreakMode];
cell.Manufactlbl.frame = CGRectMake(cell.Manufactlbl.frame.origin.x, cell.Manufactlbl.frame.origin.y, cell.Manufactlbl.frame.size.width, lLabelSIze.height);
cell.Manufactlbl.font=[UIFont systemFontOfSize:12];
cell.Manufactlbl.text=coffeeObj.price;
cell.Typelbl.frame=CGRectMake(cell.Manufactlbl.frame.origin.x + cell.Manufactlbl.frame.size.width, cell.Typelbl.frame.origin.y, cell.Typelbl.frame.size.width, cell.Typelbl.frame.size.height);
cell.Typelbl.font=[UIFont systemFontOfSize:12];
cell.Typelbl.text=coffeeObj.instrument;`

但这不起作用.

推荐答案

试试这个.

CGSize size = [@"Your string" 
               sizeWithFont:[UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:22] 
               constrainedToSize:CGSizeMake(500, CGFLOAT_MAX)];

 _Lable.frame = CGRectMake(05,05, 50, size.height);  

希望对你有帮助..

这篇关于如何根据String调整UILabel宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆