首页 文章

如何在UILabel上以编程方式sizeToFit宽度和高度?

提问于
浏览
0

我是以编程方式创建多行UILabels( [label setNumberOfLines:0]; ) .

UILabel的内置 sizeToFit 方法适用于1行UILabel,但对于多行文本,它适当地设置高度,但宽度设置得太小,导致更长的文本行换行 .

在用户输入文本之前,我不知道标签宽度 . 我想调整标签的大小以适应最长文本行的宽度 . 根据@ DonMag的评论,我还想限制标签不要比屏幕宽 .

我尝试了不同的 lineBreakMode 设置,但没有't a ' nowrap'选项 .

我搜索过SO并且有许多相关的解决方案,但没有一个解决了sizeToFit的宽度和高度问题 .

有没有办法以编程方式调整多行UILabel的大小以适应文本的宽度和高度?

2 回答

  • 0

    你可以用 boundingRectWithSize 做到这一点......

    将您的标签添加到视图中并为其指定一个起始宽度约束(并不重要,因为它将被更改) .

    保持对宽度约束的引用(如果您使用IB,IBOutlet可以正常工作) .

    不要给它一个高度限制 .

    设置标签的文本时,可以使用它来更改其宽度:

    // get the font of the label
    UIFont *theFont = _theLabel.font;
    
    // get the text of the label
    NSString *theString = _theLabel.text;
    
    // calculate the bounding rect, limiting the width to the width of the view
    CGRect r = [theString boundingRectWithSize:CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)
                                       options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                    attributes:@{NSFontAttributeName: theFont}
                                       context:nil];
    
    // change the constant of the constraint to the calculated width
    _theWidthConstraint.constant = ceil(r.size.width);
    
    // NOTE: If you are *not* using auto-layout, 
    // this same calculation can be used to explicitly set
    // the .frame of the label.
    

    Edit:

    根据OP的要求,可以在这里找到一个完整的,可运行的示例 - 仅使用代码,没有故事板/ IB - :https://github.com/DonMag/MultilineLabelFitWidth

    Edit 2:

    GitHub项目更新...现在包括手动框架设置 and 自动布局/约束的示例 .

  • 0

    通过一些更多的实验,我发现了一些我没有在SO中看到的技巧(但......) . 一般来说它的工作原理如下:

    • 找到最长的文本行

    • 将numberOfLines设置为1(暂时)

    • 将标签文本设置为最长的文本行

    • 调用label.sizeToFit(设置最长行的标签宽度)

    • 将numberOfLines设置为0(多行)

    • 将标签文本设置为完整的多行文本

    • 调用label.sizeToFit(设置所有行的标签高度)

    瞧!现在,您的UILabel的大小适合您的多行文本 .

    这是一个例子(GitHub上的演示项目:UILabelSizeToFitDemo):

    - (UILabel *)label = nil;
    
    - (void)updateLabel:(NSString *)notes {
        // close to the "sticky" notes color
        UIColor *bananaColor = [ViewController colorWithHexString:@"#FFFC79"];
    
        if (_label == nil) {
            _label = [[UILabel alloc] init];
            _label.numberOfLines = 0;
            _label.textColor = UIColor.blackColor;
            [_label setBackgroundColor:[bananaColor colorWithAlphaComponent:0.9f]];
            _label.textAlignment = NSTextAlignmentLeft;
    
            [self.view addSubview:_label];
        }
    
        // make font size based on screen size
        CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
        CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
        CGFloat fontSize = MIN(screenWidth,screenHeight) / 12;
        [_label setFont:[UIFont systemFontOfSize:fontSize]];
    
        // split lines
        NSArray *lines = [notes componentsSeparatedByString:@"\n"];
        NSString *longestLine = lines[0];   // prime it with 1st line
    
        // fill a temp UILabel with each line to find the longest line
        for (int i = 0; i < lines.count; i++) {
            NSString *line = (NSString *)lines[i];
    
            if (longestLine == nil || line.length > longestLine.length) {
                longestLine = line;
            }
        }
    
        // force UILabel to fit the largest line
        [_label setNumberOfLines:1];
        [_label setText:longestLine];
        [_label sizeToFit];
    
        // make sure it doesn't go off the screen
        if (_label.frame.size.width > screenWidth) {
            CGRect frame = _label.frame;
            frame.size.width = screenWidth - 20;
            _label.frame = frame;
        }
        // now fill with the actual notes (this saves the previous width)
        [_label setNumberOfLines:0];
        [_label setText:notes];
        [_label sizeToFit];
    
    
        // center the label in my view
        CGPoint center = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.size.height / 2);
        [_label setCenter:center];
    }
    

    UPDATE: 这是一个备用的完整解决方案,使用@DonMag的代码片段中的 boundinRectWithSize

    -(void)updateLabel:(NSString *)notes {
        // close to the "sticky" notes color
        UIColor *bananaColor = [ViewController colorWithHexString:@"#FFFC79"];
    
        if (_label == nil) {
            _label = [[UILabel alloc] init];
            _label.numberOfLines = 0;
            _label.textColor = UIColor.blackColor;
            _label.backgroundColor = [bananaColor colorWithAlphaComponent:0.9f];
            _label.textAlignment = NSTextAlignmentLeft;
    
            [self.view addSubview:_label];
        }
    
        // set new text
        _label.text = notes;
    
        // make font size based on screen size
        CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
        CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
        CGFloat fontSize = MIN(screenWidth,screenHeight) / 12;
        [_label setFont:[UIFont systemFontOfSize:fontSize]];
    
        // calculate the bounding rect, limiting the width to the width of the view
        CGRect frame = [notes boundingRectWithSize:CGSizeMake(self.view.frame.size.width, CGFLOAT_MAX)
                                           options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                        attributes:@{NSFontAttributeName: _label.font}
                                           context:nil];
    
        // set frame and then use sizeToFit
        [_label setFrame:frame];
        [_label sizeToFit];
    
        // center the label in my view
        CGPoint center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
        [_label setCenter:center];
    }
    

相关问题