根据内容调整UILabel的大小

我有一个UILabel,他的文字大小有属性

title.adjustsFontSizeToFitWidth = YES; 

这阻止了我使用标准方法来调整UILabel的大小。 我在这里看到另一个post,我应该使用该function

 sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode 

从这个答案: 当-adjustsFontSizeToFitWidth设置为YES时如何找出UILabel的字体大小?

现在,我不知道如何使其工作..这是实际的代码

 UIFont *font = [UIFont fontWithName:@"Marker Felt" size:200]; UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)]; title.text = @"this is a long title that should be resized"; title.font = font; title.numberOfLines = 1; title.adjustsFontSizeToFitWidth = YES; CGFloat pointSize = 0.0; CGSize size = [title.text sizeWithFont:font minFontSize:title.minimumFontSize actualFontSize:&pointSize forWidth:width lineBreakMode:title.lineBreakMode]; title.frame = CGRectMake(title.frame.origin.x, title.frame.origin.y, size.width, size.height); 

UILabel得到错误的调整,就好像字体大小仍然是200 ..任何线索? 谢谢!

我build议把这个提交为一个bug。

-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:返回的大小-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:具有正确的宽度,但不是高度不-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:实际的字体大小。

UILabel似乎也有这个错误。 更改标签大小以匹配由-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:返回的字体中文本的高度-sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:将错误地垂直放置标签内的文本。

解决方法是计算正确的高度,并将标签上的字体更改为实际的字体大小:

 CGFloat pointSize = 0.0f; CGRect frame = title.frame; frame.size = [title.text sizeWithFont:font minFontSize:title.minimumFontSize actualFontSize:&pointSize forWidth:width lineBreakMode:title.lineBreakMode]; UIFont *actualFont = [UIFont fontWithName:@"Marker Felt" size:pointSize]; CGSize sizeWithCorrectHeight = [title.text sizeWithFont:actualFont]; frame.size.height = sizeWithCorrectHeight.height; title.frame = frame; title.font = actualFont; 

我有一些你可以在我的github上使用的代码,检查出来,这是一个UILabel的类别,你需要设置框架的宽度,当你可以在UILabel resizeToFit,它调整高度以适应内容,并返回y标签末尾的位置,以便您可以调整出现在其后面的任何内容。

https://gist.github.com/1005520

尝试使用较小的fontSize创build字体 。 例如:

 UIFont *font = [UIFont fontWithName:@"Marker Felt" size:20]; 

但到actualFontSize传递链接到CGFloat == 200。

更新:

那么试试这个:

 UIFont *font = [UIFont fontWithName:@"Marker Felt" size:20]; UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, width, 20.0)]; title.text = @"this is a long title that should be resized"; title.font = font; title.numberOfLines = 1; title.adjustsFontSizeToFitWidth = YES; CGFloat pointSize = 0.0; CGSize size = [title.text sizeWithFont:font minFontSize:title.minimumFontSize actualFontSize:&pointSize forWidth:width lineBreakMode:title.lineBreakMode]; title.frame = CGRectMake(title.frame.origin.x, title.frame.origin.y, size.width, size.height); font = [UIFont fontWithName:@"Marker Felt" size:200]; title.font = font; 
 UILabel * label = [[UILabel alloc] init]; [label setNumberOfLines:0]; label.text=[detailDict valueForKey:@"method"]; [label setFont:[UIFont fontWithName:@"Georgia" size:16.0]]; CGSize labelsize=[label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(250, 1000.0) lineBreakMode:UILineBreakModeWordWrap]; int y=0; label.frame=CGRectMake(38, y, 245, labelsize.height); [label setBackgroundColor:[UIColor clearColor]]; [label setTextColor:[UIColor whiteColor]]; scrollView.showsVerticalScrollIndicator = NO; y+=labelsize.height; [scrollView setContentSize:CGSizeMake(200,y+50)]; [scrollView addSubview:label]; [label release]; 

我认为你应该使用这个代码,我使用的滚动视图上的标签大文本,你也可以这样做

你有没有尝试intrinsicContentSize?

  myLable.numberOfLines = 0 myLable.frame = CGRect(x: 10, y: 10, width: 300, height: lblSuperscript.intrinsicContentSize().height)