根据文本dynamic获取UILabel的高度为iOS 7.0和iOS 6.1返回不同的值

我正在使用这种方法获取UILabeldynamic的高度:

+(CGSize) GetSizeOfLabelForGivenText:(UILabel*)label Font:(UIFont*)fontForLabel Size: (CGSize)LabelSize{ label.numberOfLines = 0; CGSize labelSize = [label.text sizeWithFont:fontForLabel constrainedToSize:LabelSize lineBreakMode:NSLineBreakByCharWrapping]; return (labelSize); } 

有了这个解决scheme,我得到了UILabel的确切大小,如果我的代码在iOS 8下运行,但如果我在iOS7上运行我的应用程序,那么它返回一个不同的值。

基本上你必须dynamic设置frame.Like如下所示:

 //Compatible for both ios6 and ios7. CGSize constrainedSize = CGSizeMake(self.resizableLable.frame.size.width , 9999); NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"HelveticaNeue" size:11.0], NSFontAttributeName, nil]; NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"textToShow" attributes:attributesDictionary]; CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil]; if (requiredHeight.size.width > self.resizableLable.frame.size.width) { requiredHeight = CGRectMake(0,0, self.resizableLable.frame.size.width, requiredHeight.size.height); } CGRect newFrame = self.resizableLable.frame; newFrame.size.height = requiredHeight.size.height; self.resizableLable.frame = newFrame; 

这是一个宽度和高度的完整解决scheme。 把这些放在你的AppDelegate中:

 +(void)fixHeightOfThisLabel:(UILabel *)aLabel { aLabel.frame = CGRectMake(aLabel.frame.origin.x, aLabel.frame.origin.y, aLabel.frame.size.width, [AppDelegate heightOfTextForString:aLabel.text andFont:aLabel.font maxSize:CGSizeMake(aLabel.frame.size.width, MAXFLOAT)]); } +(CGFloat)heightOfTextForString:(NSString *)aString andFont:(UIFont *)aFont maxSize:(CGSize)aSize { // iOS7 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { CGSize sizeOfText = [aString boundingRectWithSize: aSize options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes: [NSDictionary dictionaryWithObject:aFont forKey:NSFontAttributeName] context: nil].size; return ceilf(sizeOfText.height); } // iOS6 CGSize textSize = [aString sizeWithFont:aFont constrainedToSize:aSize lineBreakMode:NSLineBreakByWordWrapping]; return ceilf(textSize.height; } +(void)fixWidthOfThisLabel:(UILabel *)aLabel { aLabel.frame = CGRectMake(aLabel.frame.origin.x, aLabel.frame.origin.y, [AppDelegate widthOfTextForString:aLabel.text andFont:aLabel.font maxSize:CGSizeMake(MAXFLOAT, aLabel.frame.size.height)], aLabel.frame.size.height); } +(CGFloat)widthOfTextForString:(NSString *)aString andFont:(UIFont *)aFont maxSize:(CGSize)aSize { // iOS7 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { CGSize sizeOfText = [aString boundingRectWithSize: aSize options: (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes: [NSDictionary dictionaryWithObject:aFont forKey:NSFontAttributeName] context: nil].size; return ceilf(sizeOfText.width); } // iOS6 CGSize textSize = [aString sizeWithFont:aFont constrainedToSize:aSize lineBreakMode:NSLineBreakByWordWrapping]; return ceilf(textSize.width); } 

然后使用这个,设置标签的文本:

 label.numberOfLines = 0; label.text = @"Everyone loves Stack OverFlow"; 

并致电:

 [AppDelegate fixHeightOfThisLabel:label]; 

注意:标签的numberOfLines必须设置为0.希望有帮助。

如果您使用的是任何系统字体,则在iOS 7中更改它们的大小不同。


另外, sizeWithFont:constrainedToSize:lineBreakMode:在iOS 7中已经被弃用了。使用sizeWithAttributes:改为(如果你在iOS 7上)

接受的答案不满足我,所以我不得不在我的代码中挖掘这个:

 CGSize possibleSize = [string sizeWithFont:[UIFont fontWithName:@"HelveticaNeue" size:10] //font you are using constrainedToSize:CGSizeMake(skillsMessage.frame.size.width,9999) lineBreakMode:NSLineBreakByWordWrapping]; CGRect newFrame = label.frame; newFrame.size.height = possibleSize.height; label.frame = newFrame; 

接受的答案太长了。 您可以使用以下内容:

 +(CGSize) GetSizeOfLabelForGivenText:(UILabel*)label Font:(UIFont*)fontForLabel Size: (CGSize) constraintSize{ label.numberOfLines = 0; CGRect labelRect = [label.text boundingRectWithSize:constraintSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName:fontForLabel} context:nil]; return (labelRect.size); } 

我一直使用sizeThatFits:

 CGRect frame = myLabel.frame; CGSize constraint = CGSizeMake(CGRectGetWidth(myLabel.frame), 20000.0f); CGSize size = [myLabel sizeThatFits:constraint]; frame.size.height = size.height; myLabel.frame = frame; 

你可以试试这个

这是我最后想出来的,希望这会对你有所帮助。 我检查了iOS版本作为苹果本身在iOS 7用户界面过渡指南 ,其中涉及检查基金会框架版本,并使用#pragma来抑制弃用:iOS 7或更高的警告提出“ – (CGSize)sizeWithFont:(UIFont * )字体constrainedToSize:(CGSize)大小“。

 + (CGSize)getStringBoundingSize:(NSString*)string forWidth:(CGFloat)width withFont:(UIFont*)font{ CGSize maxSize = CGSizeMake(width, CGFLOAT_MAX); if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { // for iOS 6.1 or earlier // temporarily suppress the warning and then turn it back on // since sizeWithFont:constrainedToSize: deprecated on iOS 7 or later #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" maxSize = [string sizeWithFont:font constrainedToSize:maxSize]; #pragma clang diagnostic pop } else { // for iOS 7 or later maxSize = [string sizeWithAttributes:@{NSFontAttributeName:font}]; } return maxSize; } 

超级简单。 只要得到文本的区域,除以宽度,然后四舍五入到最适合您的字体的高度。

 + (CGFloat)heightForText:(NSString*)text font:(UIFont*)font withinWidth:(CGFloat)width { CGSize size = [text sizeWithAttributes:@{NSFontAttributeName:font}]; CGFloat area = size.height * size.width; CGFloat height = roundf(area / width); return ceilf(height / font.lineHeight) * font.lineHeight; } 

非常多的即插即用解决scheme。 我使用它在一个辅助类很多,特别是dynamic大小的UITableViewCells。

希望这将有助于他人在未来!

在iOS7中不推荐使用sizeWithFont:constrainedToSize:lineBreakMode:方法。 您应该使用sizeWithAttributes:相反。

例:

 NSDictionary *fontAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:15]}; CGSize textSize = [self.myLabel.text sizeWithAttributes:fontAttributes]; CGFloat textWidth = textSize.width; CGFloat textHeight = textSize.height; 

我有一个情况,我需要根据文本dynamic设置标签的高度。 我使用的Xcode 7.1和我的项目部署目标是7.0,但我在iOS 9模拟器上testing它,以下解决scheme为我工作。 解决scheme如下:首先你将创build一个这样的字典:

 NSDictionary *attributes = @{NSFontAttributeName:self.YOUR_LABEL.font}; 

现在我们将计算文本的高度和宽度,并传递新创build的字典。

  CGRect rect = [YOUR_TEXT_STRING boundingRectWithSize:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil]; 

然后我们将设置我们的标签的框架:

  self.YOUR_LABEL.frame = CGRectMake(self.YOUR_LABEL.frame.origin.x, self.YOUR_LABEL.frame.origin.y, self.YOUR_LABEL.frame.size.width, rect.size.height); 

这是如何成功地设置我的标签框架根据文本。

这个代码是用buttonbutton顺时针移动四个标签,使用buttonfunction:

 (void)onclick { CGRect newFrame; CGRect newFrame1 = lbl1.frame; CGRect newFrame2 = lbl2.frame; CGRect newFrame3 = lbl3.frame; CGRect newFrame4 = lbl4.frame; lbl1.frame=newFrame; lbl4.frame=newFrame1; lbl3.frame=newFrame4; lbl2.frame=newFrame3; lbl1.frame=newFrame2; }