如何调整标签大小以适应文本的长度

我在过去的质量保证中搜寻了这个解决方案,但找不到合适的解决方案。
有谁知道如何动态调整UILabel大小以适应文本长度?
我已经上传了我不想要的(第一行)和我想要的(第二行)的屏幕截图。
我会很感激任何线索,建议或代码示例。 谢谢。 在这里输入图像描述


你正在搜索的是UILabel方法sizeToFit

我可以尝试向您解释,但知道如何使用UILabel的最佳答案是:https://stackoverflow.com/a/1054681/666479


Xcode 8和iOS 10

这对于自动布局很容易。 无需在代码中做任何事情。

在这里输入图像描述

使用自动布局

使用自动布局仅固定每个标签的顶部和左侧边缘。 不要为宽度和高度添加约束。 视图的内在内容大小将照顾到这一点。

在这里输入图像描述

以下是约束条件:

在这里输入图像描述

代码没有任何特别之处。 不需要使用sizeToFit或类似的东西。

import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var labelOne: UILabel!
    @IBOutlet weak var labelTwo: UILabel!
    @IBOutlet weak var labelThree: UILabel!

    @IBAction func changeTextButtonTapped(_ sender: UIButton) {

        labelOne.text = "David"
        labelTwo.text = "met"
        labelThree.text = "her"
    }
}

笔记

  • 这个答案已经通过Xcode 8,iOS 10和Swift 3进行了重新测试。
  • 如果您想要多线调整大小,请参阅我的其他答案。
  • 感谢这个答案让我走上正轨。

  • 使用这个扩展的UILabel类:

    //
    //  UILabelExtended.h
    
    //
    //  Created by Prateek on 6/18/11.
    
    
    
    #import <Foundation/Foundation.h>
    
    /*  **********************************************************************************************
            This class inherit the class UILabel and extend the features of UILabel. 
        ********************************************************************************************** */
    @interface UILabelExtended : UILabel {
       __unsafe_unretained id  customDelegate;
        id  objectInfo;
        SEL selector;
    }
    
    @property (nonatomic,assign) SEL selector;;
    @property (nonatomic,assign) id  customDelegate;
    @property (nonatomic,retain) id  objectInfo;
    @end
    
    @interface UILabel(UILabelCategory)
    - (void)setHeightOfLabel;
    - (void)setWidthOfLabel;
    - (void)setHeightOfLabelWithMaxHeight:(float)maxHeight;
    - (void)setWidthOfLabelWithMaxWidth:(float)maxWidth ;
    @end
    

    UILabelExtended.m

    //
    //  Created by Prateek on 6/18/11.
    
    //
    
    #import "UILabelExtended.h"
    
    
    @implementation UILabelExtended
    @synthesize selector,customDelegate, objectInfo;
    
    - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        if(self.selector)
            if([self.customDelegate respondsToSelector:self.selector]) {
                [self.customDelegate performSelector:self.selector withObject:self];
                return;
            }
    }
    
    - (void)dealloc {
    
        self.customDelegate = nil;
        self.selector = NULL;
        self.objectInfo = nil;
    }
    @end
    
    
    @implementation UILabel(UILabelCategory)
    
    - (void)setHeightOfLabel {
        UILabel* label = self;
    
        //get the height of label content
        CGFloat height = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, 99999) lineBreakMode:NSLineBreakByWordWrapping].height;
        //set the frame according to calculated height
        CGRect frame = label.frame;
        if([label.text length] > 0) {
    
            frame.size.height = height;
        } 
        else {
            frame.size.height = 0;
        }
        label.frame = frame;
    }
    
    
    - (void)setWidthOfLabel {
        UILabel* label = self;
    
            //get the height of label content
        CGFloat width = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(99999, label.bounds.size.height) lineBreakMode:NSLineBreakByWordWrapping].width;
            //set the frame according to calculated height
        CGRect frame = label.frame;
        if([label.text length] > 0) {
    
            frame.size.width = width+5;
        } 
        else {
            frame.size.width = 0;
        }
        label.frame = frame;
    }
    
    - (void)setHeightOfLabelWithMaxHeight:(float)maxHeight {
        UILabel* label = self;
    
        //get the height of label content
        CGFloat height = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.bounds.size.width, maxHeight) lineBreakMode:NSLineBreakByWordWrapping].height;
        //set the frame according to calculated height
        CGRect frame = label.frame;
    
        if([label.text length] > 0) {
            if (height > maxHeight) {
                frame.size.height = maxHeight;
            }
            else {
                frame.size.height = height;
            }
    
        } 
        else {
            frame.size.height = 0;
        }
        label.frame = frame;
    }
    
    - (void)setWidthOfLabelWithMaxWidth:(float)maxWidth  {
        UILabel* label = self;
    
        //get the height of label content
        CGFloat width = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(99999, label.bounds.size.height) lineBreakMode:NSLineBreakByWordWrapping].width;
        //set the frame according to calculated height
        CGRect frame = label.frame;
        if([label.text length] > 0) {
    
            if (width > maxWidth) {
                frame.size.width = maxWidth;
            }
            else {
                frame.size.width = width;
            }
        } 
        else {
            frame.size.width = 0;
        }
        label.frame = frame;
    }
    @end
    

    使用方法:1)设置UILabel文本2) [yourLBLObj setHeightOfLabel];[yourLBLObj setWidthOfLabel]; 它会根据文字自动设置高度或宽度。

    链接地址: http://www.djcxy.com/p/28195.html

    上一篇: How to adjust a label size to fit the length of the text

    下一篇: Vertically align UILabel