7

我试图通过 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];

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

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

4

6 回答 6

11

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

self.MyLabel.sizeToFit()
self.MyLabel.layoutIfNeeded()
于 2016-03-24T14:06:47.447 回答
10

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

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

[label sizeToFit];
于 2013-12-09T04:10:10.893 回答
1

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

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 已经足够大以容纳所有文本,则调用 sizeToFit 不会改变它的大小。(不会使它变小……如果需要就变大,但不会添加更多行)

于 2013-02-07T15:20:49.290 回答
0

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];
于 2013-02-07T13:29:06.857 回答
0

看起来您没有设置 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 ];
于 2013-02-07T15:27:03.733 回答
0

我也有麻烦,我的代码是这样的:

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

只需添加框架,它就可以完美运行!

于 2015-10-27T07:16:03.000 回答