如何动态计算UILabel高度

cdu*_*dub 20 iphone objective-c uilabel ios ios8

我有以下代码:

label.numberOfLines = 0; // allows label to have as many lines as needed
label.text = @"some long text";
[label sizeToFit];
Run Code Online (Sandbox Code Playgroud)

如何获得标签的高度?

Sal*_*idi 53

使用以下方法计算动态UILabel高度:

- (CGFloat)getLabelHeight:(UILabel*)label
{
    CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
    CGSize size;

    NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
    CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                                  options:NSStringDrawingUsesLineFragmentOrigin
                                               attributes:@{NSFontAttributeName:label.font}
                                                  context:context].size;

    size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

    return size.height;
}
Run Code Online (Sandbox Code Playgroud)


Fab*_*ger 53

获得高度的最简单方法是sizeThatFits.像这样使用它:

Objective-C的

CGFloat maxLabelWidth = 100;
CGSize neededSize = [label sizeThatFits:CGSizeMake(maxLabelWidth, CGFLOAT_MAX)];
Run Code Online (Sandbox Code Playgroud)

Swift 3.0

let maxLabelWidth: CGFloat = 100
let neededSize = label.sizeThatFits(CGSize(width: maxLabelWidth, height: CGFloat.greatestFiniteMagnitude))
Run Code Online (Sandbox Code Playgroud)

标签所需的高度neededSize.height.
请注意,我使用CGFLOAT_MAX的是尺寸高度,以确保标签有足够的位置适合CGSize.

标签的高度也取决于标签的宽度,这就是我添加的原因maxLabelWidth,如果标签宽度为100pt或200pt,则会有所不同.

希望这可以帮助!

编辑: 确保您设置label.numberOfLines = 0;否则neededSize返回文本在一行中的大小.

编辑: 添加了Swift版本,虽然命名有点奇怪,但是greatFiniteMagnitude似乎是CGFLOAT_MAX的正确等价物.


iHu*_*ulk 7

只需使用[label sizeThatFits:label.frame.size];它将返回适合给定文本的标签大小.或者你也可以关注这个问题


kis*_*747 6

对于那些想要估计标签大小的用户,可以采用一种方法来估计UICollectionView或UITableView中的标头/单元格高度,请按照以下步骤操作:

  1. 设置标签要使用的maxWidth
  2. 创建一个新的UILabel并将numberOfLines设置为0
  3. 如果使用自定义字体,则添加字体属性,例如自定义字体名称和字体大小
  4. 设置此标签的文本,并使用sizeThatFits获取估计的高度。标签高度为neededHeight.height

迅捷

let maxLabelWidth:CGFloat = collectionView.frame.width - 20
let label = UILabel()
label.numberOfLines = 0
let addressFont = [ NSFontAttributeName: UIFont(name: "OpenSans", size: 12.0)! ]
let addr = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
label.attributedText = NSMutableAttributedString(string: addr , attributes: addressFont )
let neededSize:CGSize = label.sizeThatFits(CGSizeMake(maxLabelWidth, CGFloat.max))
let labelHeight = neededSize.height
Run Code Online (Sandbox Code Playgroud)

感谢@FabioBerger