一,代码。
images/loading.gif' data-original="http://common.cnblogs.com/images/copycode.gif" />
- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. NSString *textString=@"1234567890"; NSLog(@"---height--%ld",(long)[self contentHeightWithText:textString]);}//根据要显示的text计算label高度- (NSInteger)contentHeightWithText:(NSString*)text{ NSInteger ch; //size要和label中的size一样 UIFont *font = [UIFont fontWithName:@"Arial" size:11]; //300为宽,20000.0f为高。这个需要自己设定 CGSize size = CGSizeMake(300, 20000.0f); //如果7.0以上版本。因为计算label的高度的函数变过。 if ([[UIDevice currentDevice].systemVersion doubleValue]>=7.0) { NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil]; size =[text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:tdic context:nil].size; } ch = size.height; return ch;}
二,输出。
2015-10-22 10:48:27.950 UILable高度自适应(sizeWithFont)[3232:93993] ---height--12
原标题:【代码笔记】UILable高度自适应(sizeWithFont)
关键词: