5

我正在尝试计算 UITableViewCell 的高度,所以我定义了一个看起来像这样的类方法

+ (CGFloat)heightWithText:(NSString *)text
{
    SizingLabel.text = text;
    [SizingLabel sizeThatFits:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX)];

    return (TOP_MARGIN + SizingLabel.frame.size.height + BOTTOM_MARGIN);
}

我已经像这样定义了 SizingLabel:

+ (void)initialize
{
    SizingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    SizingLabel.numberOfLines = 0;
    SizingLabel.lineBreakMode = NSLineBreakByWordWrapping;
}

但是,如果我在 -heightWithText: 方法中设置断点,我会注意到 SizingLabel 的尺寸永远不会改变,因此我得到的值不正确。这是为什么?

4

5 回答 5

7

如上所述,sizeThatFits:(因此sizeToFit)不适用于UILabel对象。

您最好使用首选textRectForBounds:limitedToNumberOfLines:方法:

+ (CGFloat)heightWithText:(NSString *)text
{
    resizingLabel.text = text;
    CGSize labelSize = [resizingLabel textRectForBounds:CGRectMake(0.0, 0.0, LABEL_WIDTH, CGFLOAT_MAX)
                                 limitedToNumberOfLines:0].size; // No limit

    return (TOP_MARGIN + labelSize.height + BOTTOM_MARGIN);
}
于 2014-10-08T08:02:04.267 回答
5
+ (CGFloat)heightWithText:(NSString *)text
{
    SizingLabel.text = text;
    CGSize labelSize = [SizingLabel sizeThatFits:CGSizeMake(LABEL_WIDTH, CGFLOAT_MAX)];

    return (TOP_MARGIN + labelSize.height + BOTTOM_MARGIN);
}
于 2013-10-28T06:52:07.887 回答
3

在您的自定义单元类中执行此操作:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

        //Message Label
        lbl_name = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 25)];            

        [lbl_name setFont:[UIFont fontWithName:@"Helvetica" size:16.0f]];
        lbl_name.lineBreakMode = UILineBreakModeWordWrap;
        lbl_name.numberOfLines = 0;
        [lbl_name sizeToFit];
        [self.contentView addSubview:lbl_name];

        //Time 
    }
    return self;
}    

-(void)resizeNameLabel:(NSString *)text
{
    CGSize constraint = CGSizeMake(300 , 20000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:16.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    [lbl_name setFrame:CGRectMake(10, 5, 300, MAX(size.height, 25.0f))];//300 Label Width
    [lbl_name setText:text];
}

在主课上这样做..

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) 
    {
        cell = (CustomCell *)[[CustomCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
        [((CustomCell *)cell) resizeNameLabel:text];

    return cell;
}

就这样吧……

于 2013-10-28T07:17:40.163 回答
0

下面的代码是计算动态文本长度的矩形(ios7版本)

- (CGRect)labelFrameWithText:(NSString *)text
{
    CGRect rect;

    // the font of your text
    UIFont *font = [UIFont systemFontOfSize:15.0]; 
    NSDictionary *attributes = @{NSFontAttributeName: font};

    // the first parametric CGSize is the max size that the rect's size can be
    CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(youImageWidth, 100.0)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];

    //the rect of the UILabel
    //This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
    rect = CGRectMake(yourLabelOriginX,
                      yourLabelOriginY,
                      ceil(boundingRect.size.width),
                      ceil(boundingRect.size.height));

    return rect;
}

在你得到矩形后,用它来计算你的单元格大小

----------------旧版本--------------------------------

CGSize contentSize = [content sizeWithFont:font
                             constrainedToSize:CGSizeMake(maxWidth, maxHeight)
                                 lineBreakMode: NSLineBreakByWordWrapping];
于 2013-10-28T06:55:46.413 回答
0

对于具有属性文本的 UILabel 的简单大小调整,我将此类别添加到 UILabel。

@implementation UILabel (PAUtils)

- (CGSize)jb_attributedSizeThatFits:(CGSize)size
{
    CGRect textRect = [self.attributedText boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];
    return textRect.size;
}

@end

有两点需要注意:

  1. 当不设置attributedText(而只是使用text)时,此方法不考虑该numberOfLines属性。
  2. boundingRectWithSize返回分数的分数。如果您将此方法返回的值用于布局,您可能应该ceilf使用宽度和高度。
于 2014-02-13T21:59:02.427 回答