NSString sizeWithFont:constrainedToSize:计算错误的高度

时间:2013-03-27 08:46:00

标签: ios uitableview nsstring uilabel sizewithfont

我有一个字幕样式UITableViewCell,其高度根据每个字段的文本长度动态变化。问题是如果标签有多行,textLabel的高度(CGSize大小)不会增加。

http://s10.postimg.org/6urher7s9/text_Label.png

奇怪的是,detailTextLabel的高度正在增加(CGSize size2)。计算两个高度的代码是相同的。

http://s21.postimg.org/m0af4gyw7/detail.png

这是我的功能:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    SETLISTFMNS0Song *song = [[[selectedSetlist.sets objectAtIndex:indexPath.section] songs]objectAtIndex:indexPath.row];
    CGSize size = [song.name sizeWithFont:[UIFont fontWithName:setlistFont size:labelFontSize] constrainedToSize:CGSizeMake(self.setsTable.bounds.size.width, CGFLOAT_MAX)];
    NSLog(@"Label: \"%@\" \tLabel Size: %f W %f H", song.name, size.width, size.height);

    NSMutableString *detail;
    if ([song cover]) {
        detail = [[NSMutableString alloc] initWithFormat:@"(%@ cover)", [[song cover] name]];
    }
    if ([song with]) {
        if (!detail) {
            detail = [[NSMutableString alloc] initWithFormat:@"(with %@)", [[song with] name]];
        }
        else {
            [detail appendFormat:@" (with %@)", [[song with] name]];
        }
    }
    if ([song info]) {
        if (!detail) {
            detail = [[NSMutableString alloc] initWithFormat:@"(%@)", [song info]];
        }
        else {
            [detail appendFormat:@" (%@)", [song info]];
        }
    }

    if (detail.length != 0) {
        CGSize size2 = [detail sizeWithFont:[UIFont fontWithName:setlistFont size:detailFontSize] constrainedToSize:CGSizeMake(self.setsTable.bounds.size.width, CGFLOAT_MAX)];
        size.height += size2.height;
        NSLog(@"Detail Label: \"%@\" \tDetail Label Size: %f W %f H", detail, size2.width, size2.height);
    }

    return size.height + 5;
}

我还在cellForRowAtIndexPath中将textLabel的numberOfLines属性设置为0以支持多行:

cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;

更新:感谢@josh我现在明白为什么会这样。我将宽度约束设置为UITableView的宽度,这太宽了。任何人都知道如何在创建之前找到UILabel的宽度? HA!

谢谢!

2 个答案:

答案 0 :(得分:1)

你计算大小的字符串有多长?我问,因为你的sizeWithFont:constrainedToSize:函数限制了单元格的整个宽度,但你的标签可能不是单元格的整个宽度。

我怀疑发生的事情是song.info足够短,如果线条是单元格的整个宽度,它会适合一行,并且你的歌曲detail足够长以至于它正在计算正确的行数,但不能超过计算的高度。

所有这些都说,我认为您需要做的是找出textLabeldetailTextLabel的宽度,并将约束设置为这些值。

更新 - 计算标签宽度的方法

由于单元格内部标签的宽度取决于单元格的宽度,并且由于在调用heightForRowAtIndexPath:时未创建单元格,因此我们需要提出一种方法来了解标签在设置宽度之前的宽度。唯一的方法是自己设置宽度。我就是这样做的:

MyCell.h

#import <UIKit/UIKit.h>

@interface MyCell : UITableViewCell

+ (CGFloat)textLabelWidthForCellOfWidth:(CGFloat)cellWidth;

@end

MyCell.m

#import "MyCell.h"

@implementation MyCell

- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect frame = self.textLabel.frame;
    frame.size.width = [MyCell textLabelWidthForCellOfWidth:self.frame.size.width];
    self.textLabel.frame = frame;
}

+ (CGFloat)textLabelWidthForCellOfWidth:(CGFloat)cellWidth {
    // This calculation can be as complex as necessary to account for all elements that affect the label
    return cellWidth - 20;
}

@end

然后在heightForRowAtIndexPath:实现中,您可以调用相同的类方法:

CGFloat labelWidth = [MyCell textLabelWidthForCellOfWidth:self.tableView.frame.size.width]; // Since non-grouped cells are the full width of the tableView
CGSize size2 = [detail sizeWithFont:[UIFont fontWithName:setlistFont size:detailFontSize] constrainedToSize:CGSizeMake(labelWidth, CGFLOAT_MAX)];

您将为需要引用的每个标签创建单独的类方法(+)。

答案 1 :(得分:0)

看着你的代码我发现你缺少定义“lineBreakMode”。

CGSize theSize = [song.name sizeWithFont:[UIFont fontWithName:setlistFont size:labelFontSize] constrainedToSize:CGSizeMake(self.setsTable.bounds.size.width, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];

用此更新您的代码。我希望它能解决你的问题。祝你好运