在 iOS 7 中动态调整标签大小 [英] Dynamically resize label in iOS 7
本文介绍了在 iOS 7 中动态调整标签大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 iOS 6 中,我使用:
In iOS 6, I am using :
CGSize labelSize = [self.text sizeWithFont:self.font constrainedToSize:size lineBreakMode:self.lineBreakMode];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , labelSize.width, self.frame.size.height);
动态调整 UILabel 的大小.这在 iOS 7 中不起作用,所以我尝试了:
To dynamically resize a UILabel. This does not work in iOS 7 so I tried:
NSString *text = self.text;
CGFloat width = size.width;
UIFont *font = self.font;
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
attributes:@{ NSFontAttributeName: font }];
CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
options:NSStringDrawingUsesDeviceMetrics
context:nil];
CGSize size = rect.size;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y , size.width, self.frame.size.height);
这是在 UILabel 上的一个类别内,但这也不起作用......任何想法我应该使用什么?
This is inside a category on UILabel, but this is not working also... Any ideas what I should be using?
推荐答案
尝试这样的事情(无需自动布局):
Try something like this (working without auto-layout) :
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"FontName" size:15], NSFontAttributeName,
nil];
CGRect frame = [label.text boundingRectWithSize:CGSizeMake(263, 2000.0)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributesDictionary
context:nil];
CGSize size = frame.size;
这篇关于在 iOS 7 中动态调整标签大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文