调整UILabel以适应?

时间:2009-01-02 07:00:37

标签: cocoa-touch

如何从iPhone Developer's Cookbook第6章的“09a-PrefsTable”食谱中修改以下片段(在tableView:cellForRowAtIndexPath: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子视图和“单元格”UITableViewCell的大小,使其大小足以适应文本(并使用或多或少的文本以及其他类型的文本对齐)?我查看了UILabel textRectForBounds:limitedToNumberOfLines:方法,但是文档声明它不应该直接调用(并且只应该被覆盖)。我尝试使用UIView sizeToFit方法,但没有成功。

更新: I asked a new question about my problem with the NSString -sizeWithFont:forWidth:lineBreakMode: method

9 个答案:

答案 0 :(得分:98)

我必须这样做,以便我扩展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];

答案 1 :(得分:17)

您应该使用NSString的-sizeWithFont:forWidth:lineBreakMode:方法检索标签的关联大小调整指标。

答案 2 :(得分:10)

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

答案 3 :(得分:5)

NSString的{​​{1}}实际上并没有执行自动换行。而是使用-sizeWithFont:forWidth:lineBreakMode:来获取字符串的准确宽度和高度值。

答案 4 :(得分:4)

试试这个:

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

答案 5 :(得分:2)

此外,您还需要实现UITableViewDelegate方法:

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

让它返回为调整大小的文本字段调整的总单元格高度。

另一个注意事项 - 如果您将前面提到的行数设置为0,则尺寸拟合实际上应该有效。它会返回一个尺寸,其高度增加,以适应标签中设置的自动换行文本,宽度设置为原始标签宽度。

这对你没有帮助,因为你需要在获得单元格之前获得heightForRow的大小,所以最好计算所需的高度(并且很可能缓存该计算以免减慢表格渲染)< / p>

答案 6 :(得分:1)

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

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

答案 7 :(得分:0)

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

我只是在tableView:cellForRowAtIndexPath:中设置了文字。而不是tableView:heightForRowAtIndexPath:我回来了

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

我长串。最后有一个宽大的Cell,标签上有4行文字。

答案 8 :(得分:0)

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

在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;
相关问题