Cocoa touch 调整UILabel的大小以适合?

Cocoa touch 调整UILabel的大小以适合?,cocoa-touch,Cocoa Touch,如何修改iPhone开发者菜谱第6章“09a-PrefsTable”配方中的以下代码段(在tableView:CellForRowatineXpath:UITableViewController方法中): if (row == 1) { // Create a big word-wrapped UILabel cell = [tableView dequeueReusableCellWithIdentifier:@"libertyCell"]; if (!cell)

如何修改iPhone开发者菜谱第6章“09a-PrefsTable”配方中的以下代码段(在tableView:CellForRowatineXpath:UITableViewController方法中):

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子视图和“cell”UITableViewCell的大小,使其刚好适合文本(并处理更多或更少的文本以及其他类型的文本对齐方式),请执行以下操作?我查看了UILabel textRectForBounds:limitedToNumberOfLines:方法,但文档说明不应直接调用它(并且只应重写它)。我尝试了UIView sizeToFit方法,但没有成功


更新:

您应该使用NSString的
-sizeWithFont:forWidth:lineBreakMode:
方法来检索标签的相关大小度量。

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

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
并返回调整大小的文本字段的总单元格高度

另一个注意事项是,如果前面提到的行数设置为0,那么适合的大小实际上应该可以工作。它将返回一个大小,高度增加以适应标签中设置的单词包装文本,宽度设置为原始标签宽度的任何值


但这对您没有帮助,因为您需要在获取单元格之前获取heightForRow中的大小,因此最好计算所需的高度(并且很可能缓存该计算,以免减慢表的渲染速度)

NSString
-sizeWithFont:forWidth:lineBreakMode:
实际上不执行换行。相反,请使用
-sizeWithFont:constrainedToSize:lineBreakMode:
获取字符串的准确宽度和高度值。

尝试以下操作:

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]; 

我必须做到这一点,我扩展了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 SizeToFitedFixedWidth:kSomeFixedWidth]

以下是我使用的一些代码:

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

我也有类似的问题,我有一个UITableViewCell,它在故事板中被设计为一个静态单元。我使用了
[super tableView:cellforrowatinexpath:
来获取它。所以我想调整UILabel“detailTextLabel”的大小,使其适合我设置的文本。风格是“正确的细节”

我只是在我的
表格视图中设置了文本:cellforrowatinexpath:
。然后在
tableView:heightforrowatinexpath:
I返回

UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.detailTextLabel.frame.size.height
我有一根长长的绳子。最后是一个宽单元格,标签上有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;

Kevin,如果您将答案更新为-[NSString sizeWithFont:forWidth:lineBreakMode:]我会接受。在swift中,您应该使用
sizeWithAttributes
方法刚才看到了指向其他问题的链接。看起来你就在那里-很抱歉被骗了。选择+1作为示例句子。Magic。将帧的高度设置为0-然后调用[self sizeToFit];Thx。设置边界而不是帧不是更好吗?边界也可以工作,它只是帧的移动。由于我将x和y设置为self.frame.origin值,因此它与self.bounds=CGRectMake(0,0,fixedWidth,fixedHeight)实现了相同的功能,如果您可以直接设置frame.size属性就好了,但这不起作用:)谢谢,效果很好!,虽然ios 6还有其他选择,但我的应用程序是ios 5+,似乎ios 6中已经不推荐使用UILineBreakModeWordWrap。OscarGomez还有什么其他选择?