调整UILabel以适应?

如何修改下面的代码片段(在tableView:cellForRowAtIndexPath:UITableViewController方法中)来自iPhone Developer's Cookbook第6章的“09a – PrefsTable”

if (row == 1) { // Create a big word-wrapped UILabel cell = [tableView dequeueReusableCellWithIdentifier:@"libertyCell"]; if (!cell) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"libertyCell"] autorelease]; [cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(20.0f, 10.0f, 280.0f, 330.0f)]]; } UILabel *sv = [[cell subviews] lastObject]; sv.text =  @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; sv.textAlignment = UITextAlignmentCenter; sv.lineBreakMode = UILineBreakModeWordWrap; sv.numberOfLines = 9999; return cell; } 

…调整“sv”UILabel子视图和“单元格”UITableViewCell大小的大小,以适应文本(和更多或更less的文本和其他types的文本alignment)? 我查看了UILabel textRectForBounds:limitedToNumberOfLines:方法,但文档指出不应该直接调用它(只应该被覆盖)。 我尝试了UIView sizeToFit方法,但没有成功。

更新: 我问了一个关于我的问题与NSString -sizeWithFont:forWidth:lineBreakMode:方法的新问题 。

我不得不这样做,我扩展了UILabel为我做的:

 @interface UILabel (BPExtensions) - (void)sizeToFitFixedWidth:(CGFloat)fixedWidth; @end @implementation UILabel (BPExtensions) - (void)sizeToFitFixedWidth:(CGFloat)fixedWidth { self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0); self.lineBreakMode = NSLineBreakByWordWrapping; self.numberOfLines = 0; [self sizeToFit]; } @end 

然后有一个标签有一个可变的多行高度,但只有一个固定的宽度:

[myLabel sizeToFitFixedWidth:kSomeFixedWidth];

您应该使用NSString的-sizeWithFont:forWidth:lineBreakMode:方法来检索标签的关联尺寸度量标准。

此外,如果要使用该代码,请将numberOfLines属性更改为0

NSString-sizeWithFont:forWidth:lineBreakMode:实际上并不执行单词换行。 相反,使用-sizeWithFont:constrainedToSize:lineBreakMode:为string获取准确的宽度和高度值。

尝试这个:

 sv.text = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; sv.textAlignment = UITextAlignmentCenter; sv.lineBreakMode = UILineBreakModeWordWrap; sv.numberOfLines = 0; [sv sizeToFit]; 

另外,您将需要实现UITableViewDelegate方法:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 

并让它返callback整大小的文本字段的总单元格高度。

另一个注意事项 – 如果您的行数设置为0,则前面提到的“适合resize”应该可以工作。 它会给你一个大小的高度增加,以适应标签中的单词包装文本设置和宽度设置为任何原始标签宽度了。

这不会帮助你,因为在获取单元格之前需要在heightForRow中获取大小,所以你最好计算所需的高度(并且很可能caching该计算以便减慢表格渲染的速度)

这是我使用的一些代码:

 CGSize textSize = [myLabel.text sizeWithFont:myLabel.font]; 

我有类似的问题,我有一个在StoryBoards中devise的UITableViewCell作为静态单元格。 我用[super tableView:cellForRowAtIndexPath:]来得到它。 所以我想调整UILabel“detailTextLabel”的大小,使其适合我设置的文本。 风格是“正确的细节”。

我只是在我的tableView:cellForRowAtIndexPath:设置文本。 而在tableView:heightForRowAtIndexPath:我回来了

 UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; return cell.detailTextLabel.frame.size.height 

我有很长的string。 最后有一个宽的单元格,标签中有4行文字。

我有类似的问题。 我解决了这个问题

在cellForRowAtIndexPath方法设置字体大小为任何你想要的。

 cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; cell.textLabel.numberOfLines = 0; [cell.textLabel setFont:[UIFont systemFontOfSize:14.0]]; [cell.textLabel sizeToFit]; 

并在heightForRowAtIndexPath方法中增加字体大小。

  CGFloat height; UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; NSString *text = cell.detailTextLabel.text; CGSize constraint = CGSizeMake(320, 20000.0f); CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; CGFloat calHeight = MAX(size.height, 44.0f); height = calHeight + (CELL_CONTENT_MARGIN * 2); return height;