xcode UILabel sizeToFit 无法正常工作。使用 setNumberOfLines 不起作用。需要可变高度功能才能工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14751638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UILabel sizeToFit not working properly. Using setNumberOfLines doesn't work. Need variable height function to work
提问by user2050812
I am trying to show a variable height amount of text through UILabel.
I set everything up through a combination of Xcode's storyboard and coding directly into the implementation files.
我正在尝试通过 UILabel 显示可变高度的文本量。
我通过 Xcode 的故事板和直接编码到实现文件中的组合来设置所有内容。
Here is the code:
这是代码:
CGRect labelFrame = CGRectMake(20, 20, 280, 800);
descriptionLabel.frame = labelFrame;
NSString *description = self.spell[@"description"];
descriptionLabel.text = description;
[descriptionLabel setNumberOfLines:0];
[descriptionLabel sizeToFit];
I have tried changing the wordwrap functions and tried playing around with a lot of different settings but to no avail.
我尝试更改自动换行功能并尝试使用许多不同的设置但无济于事。
Is there something specific that needs to be done in the storyboard?
Is the structure of my view->label important?
I have it so that the labels are in a view (which takes up the whole screen).
在故事板中是否需要做一些具体的事情?
我的视图->标签的结构重要吗?
我有它,以便标签在视图中(占据整个屏幕)。
回答by 1actobacillus
You do not need to calculate the heights manually. For variable height, set the height of the label to 0 before calling sizeToFit
, like this.
您不需要手动计算高度。对于可变高度,在调用之前将标签的高度设置为 0 sizeToFit
,就像这样。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
label.numberOfLines = 0;
label.text = @"Some long piece of text...";
[label sizeToFit];
回答by Ken Seow
I'm using SWIFT and I faced the same issue. I fixed it after I added layoutIfNeeded()
. My code is as follows:
我正在使用 SWIFT,但遇到了同样的问题。添加后我修复了它layoutIfNeeded()
。我的代码如下:
self.MyLabel.sizeToFit()
self.MyLabel.layoutIfNeeded()
回答by Andrei Filip
Here is my solution for any problem such as yours (dynamic height):
这是我对任何问题(例如您的问题(动态高度))的解决方案:
NSString *description = @"Some text";
CGSize dynamicSize = [[description sizeWithFont:[UIFont fontWithName:@"FONT_NAME" size:14] constrainedToSize:CGSizeMake(300, 10000)]; //the width should be the one you need and just put in a very big height so that anything you would put in here would fit
//dynamicSize is now the exact size you will need, with the fixed width and the height just enough so that all the text will fit.
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake:(0, 0, dynamicSize.width, dynamicSize.height)];
someLabel.font = YOUR_FONT;
someLabel.numberOfRows = 10 //You should calculate how many rows you need by dividing dynamicSize.height to the height of one row (should also take into account the padding that your UILabel will put between rows
This is, in my opinion the safest way to determine the needed height. Also, as far as I know, calling sizeToFit will not change it's size if it is already big enough to hold all the text.(will not make it smaller...just bigger if it needs to but it will not add more lines)
在我看来,这是确定所需高度的最安全方法。另外,据我所知,如果它已经足够大以容纳所有文本,则调用 sizeToFit 不会改变它的大小。(不会让它变小......如果需要,它会变大但不会添加更多行)
回答by Paul Cezanne
It doesn't look like you are setting lineBreakMode.
看起来您没有设置 lineBreakMode。
I got some code from stackoverflow way back when and put it into a category, you can about it here: http://www.notthepainter.com/multi-line-uilabel/
我从 stackoverflow 上得到了一些代码,并将其放入一个类别中,您可以在这里了解它:http: //www.notthepainter.com/multi-line-uilabel/
But if you just want the code, here it is:
但如果你只想要代码,这里是:
@interface UILabel (BPExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth;
@end
?
@implementation UILabel (EnkiExtensions)
- (void)sizeToFitFixedWidth:(CGFloat)fixedWidth
{
????if (fixedWidth < 0) {
????????self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
????} else {
????????self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
????}
????self.lineBreakMode = UILineBreakModeWordWrap;
????self.numberOfLines = 0;
????[self sizeToFit];
}
@end
You call it like this:
你这样称呼它:
helpLabel_.text = @"You need to cut the blue wire and ground the red wire. Do not cut the red wire and ground the blue wire, that would be bad.";
[helpLabel_ sizeToFitFixedWidth: desiredWidth ];
回答by Jim
I tried to create the same thing and it works completely fine with me. (I don't have storyboard in my app but i don't think it will affect anything.)
我试图创造同样的东西,它对我来说完全正常。(我的应用程序中没有故事板,但我认为它不会影响任何事情。)
UILabel *bioLabel = [[UILabel alloc] init];
CGRect labelFrame = CGRectMake(0, 100, 320, 500);
bioLabel.frame = labelFrame;
NSString *bio =@"YOUR_TEXT_HERE";
bioLabel.text = description;
[bioLabel setNumberOfLines:0];
[bioLabel sizeToFit];
[self.view addSubview:bioLabel];
回答by Mango
I have the troubel too,my code like this:
我也有麻烦,我的代码是这样的:
TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] init];
[tagBtn setTitle:tagModel.values forState:UIControlStateNormal];
[tagBtn.titleLabel sizeToFit];
TeacherTagBtn subclass of UIbutton you knows! then i want to get the width of label like this:
你知道的 UIbutton 的 TeacherTagBtn 子类!然后我想像这样获得标签的宽度:
tempBtn.titleLabel.frame.size.width
Finally, I failed. the width is 0. so like this:
最后,我失败了。宽度是 0. 所以像这样:
TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] initWithFrame:CGRectMake(0, 0, 100, 16)];
[tagBtn setTitle:tagModel.values forState:UIControlStateNormal];
[tagBtn.titleLabel sizeToFit];
only add frame, it works perfectly!
只添加框架,它完美地工作!