根据文本长度更改单元格高度

时间:2014-04-22 17:24:07

标签: ios ios7 uitableview

我在根据文字长度更改单元格高度时遇到问题。我正在尝试使用以下代码计算单元格高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self dynamicHeightAtIndexPath:indexPath] + 32;
}

- (CGFloat)dynamicHeightAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize maximumSize = CGSizeMake(275, 9999);
    UIFont *myFont = [UIFont fontWithName:@"Avenir-Light" size:15.0];
    CGSize stringsize = [[[self.userAdsArray objectAtIndex:indexPath.row] valueForKey:@"name"]
                         sizeWithFont:myFont
                         constrainedToSize:maximumSize
                         lineBreakMode:NSLineBreakByWordWrapping];
    return stringsize.height;
}

我得到了这些结果(从顶部开始的第5个细胞被打破):

enter image description here

3 个答案:

答案 0 :(得分:0)

请改为尝试:

- (CGFloat)dynamicHeightAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *myText = [[self.userAdsArray objectAtIndex:indexPath.row] valueForKey:@"name"];
    CGRect textSize = [myText boundingRectWithSize:CGSizeMake(275.0f, CGFLOAT_MAX)
                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                       attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Avenir-Light" size:15.0f]}
                       context:nil];
    return fabs(textSize.size.height);
}

不确定密钥@"name"的对象具有哪种类型,但上述代码适用于NSString。因此,声明是为了清晰起见。

答案 1 :(得分:0)

我只是将文本高度计算功能包装到NSString的类别。

.h文件中:

@interface NSString (Additions)

- (CGSize)sizeWithFont:(UIFont *)font;
- (CGSize)sizeWithFontSize:(float)fSize constrainedToSize:(CGSize)cSize;
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)cSize;

@end

.m文件中:

#import "NSString+Additions.h"

#define kSystemVersion ([[UIDevice currentDevice] systemVersion].intValue)

@implementation NSString (Additions)

- (CGSize)sizeWithFont:(UIFont *)font
{
    return [self sizeWithFont:font constrainedToSize:(CGSize)
            {MAXFLOAT, MAXFLOAT}];
}

- (CGSize)sizeWithFontSize:(float)fSize constrainedToSize:(CGSize)cSize
{
    UIFont *font = [UIFont systemFontOfSize:fSize];
    return [self sizeWithFont:font constrainedToSize:cSize];
}

- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)cSize
{
    if (kSystemVersion < 7)
    {
        CGSize size = [self sizeWithFont:font constrainedToSize:cSize
                       lineBreakMode:NSLineBreakByWordWrapping];
        return size;
    }
    else
    {
        NSDictionary *stringAttributes = @{NSFontAttributeName:font};
        CGRect rect = [self boundingRectWithSize:cSize
                       options:NSStringDrawingUsesLineFragmentOrigin |
                               NSStringDrawingUsesFontLeading
                       attributes:stringAttributes
                       context:nil];

        return rect.size;
    }
}

@end

然后,只需在您想要的位置计算字符串的高度。如:

CGSize cSize = (CGSize){THE_LABEL_WIDTH_HERE, MAXFLOAT};
NSString *tmpString = @"Hello there. I'm junkor, and you can call me jun.";

// will use the system font
float height = [tmpString sizeWithFontSize:15 constrainedToSize:cSize].height;

高度就是你想要的。有时,在计算文本大小时,可以将大小缓存到​​数据实体,例如tableView的委托函数:

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

当您重新加载数据或滚动tableView时,它总是计算,对高度(或大小)进行缓存。确保你只为每个文字计算一次,它会更好。

答案 2 :(得分:-1)

请试试这个:

- (CGFloat)dynamicHeightAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize maximumSize = CGSizeMake(275, 9999);
    UIFont *myFont = [UIFont fontWithName:@"Avenir-Light" size:15.0];

    NSString *text = [[self.userAdsArray objectAtIndex:indexPath.row] valueForKey:@"name"];
    CGFloat height = [text sizeWithFont:myFont constrainedToSize:maximumSize].height;

    return height;
}