根据文字调整UILabel高度

本文探讨了在iOS开发中如何动态调整UILabel的高度以适应不同长度的文本内容,通过设置UILabel的lineBreakMode和numberOfLines属性,并使用sizeToFit方法实现自动调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Adjust UILabel height depending on the text

Consider I have the following text in a UILabel (a long line of dynamic text): 考虑我在UILabel有以下文本(一长串动态文本):

Since the alien army vastly outnumbers the team, players must use the post-apocalyptic world to their advantage, such as seeking cover behind dumpsters, pillars, cars, rubble, and other objects. 由于外星军队的数量远远超过了团队,因此玩家必须利用后世界末日的世界,例如在垃圾箱,支柱,汽车,瓦砾和其他物体后面寻找掩护。

I want to resize the UILabel's height so that the text can fit in. I'm using following properties of UILabel to make the text within to wrap. 我想调整UILabel's高度,以便文本可以适应。我正在使用UILabel以下属性来使文本内部包装。

myUILabel.lineBreakMode = UILineBreakModeWordWrap;
myUILabel.numberOfLines = 0;

Please let me know if I'm not heading in the right direction. 如果我没有朝着正确的方向前进,请告诉我。 Thanks. 谢谢。


#1楼

参考:https://stackoom.com/question/1s85/根据文字调整UILabel高度


#2楼

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cellIdentifier = @"myCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell.myUILabel.lineBreakMode = UILineBreakModeWordWrap;        
    cell.myUILabel.numberOfLines = 0;
    cell.myUILabel.text = @"Some very very very very long text....."
    [cell.myUILabel.criterionDescriptionLabel sizeToFit];    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    CGFloat rowHeight = cell.myUILabel.frame.size.height + 10;

    return rowHeight;    
}

#3楼

You were going in the right direction. 你正朝着正确的方向前进。 All you need to do is: 你需要做的就是:

myUILabel.numberOfLines = 0;
myUILabel.text = @"Enter large amount of text here";
[myUILabel sizeToFit];

#4楼

Here is a category version: 这是一个类别版本:

UILabel+AutoSize.h #import UILabel + AutoSize.h #import

@interface UILabel (AutoSize)

- (void) autosizeForWidth: (int) width;

@end

UILabel+AutoSize.m 的UILabel + AutoSize.m

#import "UILabel+AutoSize.h"

@implementation UILabel (AutoSize)

- (void) autosizeForWidth: (int) width {
    self.lineBreakMode = UILineBreakModeWordWrap;
    self.numberOfLines = 0;
    CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
    CGSize expectedLabelSize = [self.text sizeWithFont:self.font constrainedToSize:maximumLabelSize lineBreakMode:self.lineBreakMode];
    CGRect newFrame = self.frame;
    newFrame.size.height = expectedLabelSize.height;
    self.frame = newFrame;
}

@end

#5楼

UILabel *itemTitle = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10,100, 200.0f)];
itemTitle.text = @"aseruy56uiytitfesh";
itemTitle.adjustsFontSizeToFitWidth = NO;
itemTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
itemTitle.font = [UIFont boldSystemFontOfSize:18.0];
itemTitle.textColor = [UIColor blackColor];
itemTitle.shadowColor = [UIColor whiteColor];
itemTitle.shadowOffset = CGSizeMake(0, 1);
itemTitle.backgroundColor = [UIColor blueColor];
itemTitle.lineBreakMode = UILineBreakModeWordWrap;
itemTitle.numberOfLines = 0;
[itemTitle sizeToFit];
[self.view addSubview:itemTitle];

use this here all the properties are used on the label and test it by increasing the text in the itemTitle.text as 在这里使用它在标签上使用所有属性并通过增加itemTitle.text中的文本来测试它

itemTitle.text = @"diofgorigjveghnhkvjteinughntivugenvitugnvkejrfgnvkhv";

it will show the perfetc answer as you need 它将根据您的需要显示perfetc答案


#6楼

In iOS 6 Apple has added a property to UILabel that greatly simplifies dynamic vertical resizing of labels: preferredMaxLayoutWidth . 在iOS 6中,Apple为UILabel添加了一个属性,极大地简化了标签的动态垂直大小调整: preferredMaxLayoutWidth

Using this property in combination with lineBreakMode = NSLineBreakByWordWrapping and sizeToFit method allows easily resize a UILabel instance to the height that accommodates the entire text. 将此属性与lineBreakMode = NSLineBreakByWordWrappingsizeToFit方法结合使用,可以轻松地将UILabel实例的大小调整为适应整个文本的高度。

A quote from iOS documentation: 来自iOS文档的引用:

preferredMaxLayoutWidth The preferred maximum width (in points) for a multiline label. preferredMaxLayoutWidth多行标签的首选最大宽度(以磅为单位)。

Discussion This property affects the size of the label when layout constraints are applied to it. 讨论当应用布局约束时,此属性会影响标签的大小。 During layout, if the text extends beyond the width specified by this property, the additional text is flowed to one or more new lines, thereby increasing the height of the label. 在布局期间,如果文本超出此属性指定的宽度,则附加文本将流向一个或多个新行,从而增加标签的高度。

A sample: 一个样品:

...
UILabel *status = [[UILabel alloc] init];
status.lineBreakMode = NSLineBreakByWordWrapping;
status.numberOfLines = 5; // limits to 5 lines; use 0 for unlimited.

[self addSubview:status]; // self here is the parent view

status.preferredMaxLayoutWidth = self.frame.size.width; // assumes the parent view has its frame already set.

status.text = @"Some quite lengthy message may go here…";
[status sizeToFit];
[status setNeedsDisplay];
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值