iphone关于UILabel和UITextView的适应高度

定义一个测试用的字符串,然后根据字体、UiLable或UiTextView的宽度在指定模式下算出相应的size。

  1. NSString * desc = @"Abstract:Returns the size of the string if it were rendered with the specified constraints.";  
  2. CGSize size = [desc sizeWithFont : [UIFont  systemFontOfSize:14]  //设置计算size的字体与字号                           
  3.                           constrainedToSize CGSizeMake(285, 2000)           //根据显示控件宽度不同,285需要调整                              
  4.                                lineBreakMode UILineBreakModeWordWrap];   //设置换行的截断方式  

UILable设置:

  1. UILabel *lblLabel = [[UILabel allocinitWithFrame:CGRectMake(00285,  size.height+10)];  
  2. [lblLabel setNumberOfLines : 0];                                                             //设置lable的最大行数,设置为0表示无行数限制
  3. [lblLabel setFont : [UIFont  systemFontOfSize 14]];  
  4. [lblLabel setText : desc]; 

UITextView设置:

  1. UITextView *txtView = [[UITextView allocinitWithFrame CGRectMake(00285,  size.height+10)];
  2. [txtView setFont : [UIFont systemFontOfSize 14]];               
  3. [txtView setText : desc];


转自:http://hi.baidu.com/ytugreat/blog/item/5c95c2c5a6c2db079d163da3.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值