iphone关于UILabel和UITextView的适应高度

来源:互联网 发布:泉州淘宝设计培训 编辑:程序博客网 时间:2024/06/07 05:53

    因为NSString里已经有相应的方法提供给你来进行计算了,所以实现起来只要简单的几句。

这里是关键:

    NSString * desc = @"Description it is  a test font, and don't become angry for which i use to do here.Now here is a very nice party from american or not!";    CGSize  size = [desc sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(240, 2000) lineBreakMode:UILineBreakModeWordWrap];


定义一个测试用的字符串,然后根据字体、textfiled的宽度在wordwrap模式下算出相应的size。
然后进行相应的设置:

    [lblLabel setNumberOfLines:0];    [lblLabel setFrame:CGRectMake(40, 135, 240, size.height+10)];    [lblLabel setText:desc];

注意这里uilabel的numberoflines(即最大行数限制)设置成0,即不做行数限制。

textview的类似,就不在说明了。

原创粉丝点击