UILabel sizeToFit无法正常工作。使用 setNumberOfLines 不起作用。需要可变高度功能才能工作



我正试图通过UILabel显示可变高度的文本
我通过Xcode的故事板和直接编码到实现文件中的组合来设置一切。

这是代码:

CGRect labelFrame = CGRectMake(20, 20, 280, 800);
descriptionLabel.frame = labelFrame;
NSString *description = self.spell[@"description"];
descriptionLabel.text = description;
[descriptionLabel setNumberOfLines:0];
[descriptionLabel sizeToFit];

我尝试过更改换行功能,并尝试过使用许多不同的设置,但都无济于事。

故事板中是否需要做一些具体的事情
我的视图->标签的结构重要吗
我把它放在一个视图中(它占据了整个屏幕)。

我正在使用SWIFT,但我遇到了同样的问题。我在添加layoutIfNeeded()后修复了它。我的代码如下:

self.MyLabel.sizeToFit()
self.MyLabel.layoutIfNeeded()

您不需要手动计算高度。对于可变高度,在调用sizeToFit之前将标签的高度设置为0,如下所示。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
label.numberOfLines = 0;
label.text = @"Some long piece of text...";
[label sizeToFit];

这是我对任何问题的解决方案,比如你的问题(动态高度):

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

在我看来,这是确定所需身高的最安全的方法。此外,据我所知,如果它已经足够大,可以容纳所有文本,那么调用sizeToFit不会改变它的大小。(不会使其变小……如果需要,只会变大,但不会添加更多行)

看起来并不是在设置lineBreakMode。

我很久以前就从stackoverflow那里得到了一些代码,并将其归入一个类别,你可以在这里谈论它:http://www.notthepainter.com/multi-line-uilabel/

但如果你只是想要代码,这里是:

@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

你这样称呼它:

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 ];

我试着创建同样的东西,它对我来说非常好。(我的应用程序中没有故事板,但我认为它不会影响任何事情。)

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];

我也有troubel,我的代码是这样的:

    TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] init];
    [tagBtn setTitle:tagModel.values forState:UIControlStateNormal];
    [tagBtn.titleLabel sizeToFit];

你知道UIbutton的TeacherTagBtn子类!然后我想得到这样的标签宽度:

tempBtn.titleLabel.frame.size.width

最后,我失败了。宽度为0。就像这样:

    TeacherTagBtn *tagBtn = [[TeacherTagBtn alloc] initWithFrame:CGRectMake(0, 0, 100, 16)];
    [tagBtn setTitle:tagModel.values forState:UIControlStateNormal];
    [tagBtn.titleLabel sizeToFit];

只需添加框架,就可以完美工作!

最新更新