假设我在UILabel中有以下文本(一长行动态文本):
由于外星军队的数量远远超过了团队,玩家必须利用后世界末日的优势,比如在垃圾箱、柱子、汽车、瓦砾和其他物体后面寻找掩护。
我想调整UILabel的高度,这样文本就可以放进去了。我使用以下属性的UILabel,使文本内包装。
myUILabel.lineBreakMode = UILineBreakModeWordWrap;
myUILabel.numberOfLines = 0;
如果我的方向不对,请告诉我。谢谢。
假设我在UILabel中有以下文本(一长行动态文本):
由于外星军队的数量远远超过了团队,玩家必须利用后世界末日的优势,比如在垃圾箱、柱子、汽车、瓦砾和其他物体后面寻找掩护。
我想调整UILabel的高度,这样文本就可以放进去了。我使用以下属性的UILabel,使文本内包装。
myUILabel.lineBreakMode = UILineBreakModeWordWrap;
myUILabel.numberOfLines = 0;
如果我的方向不对,请告诉我。谢谢。
当前回答
最后,它成功了。谢谢大家。
我没有让它工作,因为我试图在heightForRowAtIndexPath方法中调整标签的大小:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
和(是的,我很傻),我在cellForRowAtIndexPath方法中调整标签的默认大小-我忽略了我之前写的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
其他回答
检查这个工作完美,没有添加单行代码。使用自动布局()
我根据你的要求做了一个demo给你从下面的链接下载,
自动调整UIView和UILabel的大小
循序渐进指南:-
步骤1:-设置约束为UIView
1)领先2)领先3)落后(主视图)
步骤2:—将constraint设置为Label 1
1)领先2)顶部3)尾随(从它的父视图)
步骤3:-将constraint设置为Label 2
1)领先2)尾随(从它的父视图)
第4步:-最棘手的给按钮的UILabel从UIView。
步骤5:-(可选)将constraint设置为UIButton
1)领先2)底部3)后拖4)固定高度(从主视图开始)
输出:
注意:-确保在Label属性中设置了Number of lines =0。
我希望这个信息足以理解Autoresize UIView根据UILabel的高度和Autoresize UILabel根据文本。
你可以通过以下方式实现TableViewController的(UITableViewCell *)tableView:cellForRowAtIndexPath方法(例如):
#define CELL_LABEL_TAG 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = @"my long text";
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
}
CGFloat width = [UIScreen mainScreen].bounds.size.width - 50;
CGFloat height = [self textHeight:text] + 10;
CGRect frame = CGRectMake(10.0f, 10.0f, width, height);
UILabel *cellLabel = [[UILabel alloc] initWithFrame:frame];
cellLabel.tag = CELL_LABEL_TAG;
cellLabel.textColor = [UIColor blackColor];
cellLabel.backgroundColor = [UIColor clearColor];
cellLabel.textAlignment = UITextAlignmentLeft;
cellLabel.font = [UIFont systemFontOfSize:12.0f];
[cell.contentView addSubview:cellLabel];
[cellLabel release];
return cell;
}
UILabel *label = (UILabel *)[cell viewWithTag:CELL_LABEL_TAG];
label.text = text;
label.numberOfLines = 0;
[label sizeToFit];
return cell;
也可以使用NSString的sizeWithFont:constrainedToSize:lineBreakMode:方法来计算文本的高度。
在iOS 6中,苹果为UILabel添加了一个属性,极大地简化了标签的动态垂直大小调整:preferredmaxlayoutidth。
将此属性与lineBreakMode = nslinebreakbywordwrapped和sizeToFit方法结合使用,可以轻松地调整UILabel实例的大小,以容纳整个文本的高度。
引用iOS文档:
preferredMaxLayoutWidth 多行标签的首选最大宽度(以点为单位)。 讨论 当对标签应用布局约束时,此属性将影响标签的大小。在布局过程中,如果文本扩展到超出此属性指定的宽度,则附加文本将流动到一个或多个新行,从而增加标签的高度。
一个示例:
...
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];
...
谢谢大家的帮助,这是我尝试的代码,这是为我工作
UILabel *instructions = [[UILabel alloc]initWithFrame:CGRectMake(10, 225, 300, 180)];
NSString *text = @"First take clear picture and then try to zoom in to fit the ";
instructions.text = text;
instructions.textAlignment = UITextAlignmentCenter;
instructions.lineBreakMode = NSLineBreakByWordWrapping;
[instructions setTextColor:[UIColor grayColor]];
CGSize expectedLabelSize = [text sizeWithFont:instructions.font
constrainedToSize:instructions.frame.size
lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame = instructions.frame;
newFrame.size.height = expectedLabelSize.height;
instructions.frame = newFrame;
instructions.numberOfLines = 0;
[instructions sizeToFit];
[self addSubview:instructions];
对我来说,最简单和更好的方法是将高度约束应用于标签,并将优先级设置为低,即storyboard中的(250)。
所以你不需要担心通过编程计算高度和宽度,多亏了storyboard。