【问题标题】:UILabel sizeToFit not working properly. Using setNumberOfLines doesn't work. Need variable height function to workUILabel sizeToFit 无法正常工作。使用 setNumberOfLines 不起作用。需要可变高度功能才能工作
【发布时间】:2013-02-07 12:55:58
【问题描述】:

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

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

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

【问题讨论】:

  • 看看这个post 设置多行标签

标签: iphone xcode storyboard


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我正在为 iOS 7 开发,但这不起作用。也许在某些情况下它确实如此,但在我的情况下,一切都按照你的方式设置,加上背景颜色和更大的字体,它不起作用。它只是添加省略号。
    • 在情节提要上将标签的高度设置为 0 对我来说很快。在代码中设置框架没有效果。
    【解决方案3】:

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

    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 不会改变它的大小。(不会使它变小...如果需要就变大,但不会添加更多行)

    【讨论】:

      【解决方案4】:

      您似乎没有设置 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 ];
      

      【讨论】:

        【解决方案5】:

        我尝试创建相同的东西,它完全适合我。 (我的应用中没有情节提要,但我认为它不会影响任何事情。)

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

        【讨论】:

          【解决方案6】:

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

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

          只加框架,效果很好!

          【讨论】:

            猜你喜欢
            • 2013-12-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-05
            • 2017-09-23
            • 2017-01-08
            • 1970-01-01
            相关资源
            最近更新 更多