我正在尝试创建一个稍后将被渲染的单元格的屏幕外版本,然后使用
[cell.label sizeToFit];
然后使用标签的新调整大小来计算这样的高度:
float height = cell.complishLabel.bounds.size.height;
return 50 + height;
然而,我遇到的问题是,对于身高,我总是得到0而且我不确定为什么。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Make an off screen cell
ZSSComplishTableViewCell *cell = [[ZSSComplishTableViewCell alloc] init];
//Which section?
if (indexPath.section == 0) {
//Get a statement from the datasource and assign it to the label
NSArray *todayComplishs = [[ZSSComplishStore sharedStore] todayComplishs];
ZSSComplish *complish = todayComplishs[indexPath.row];
cell.complishLabel.text = complish.statement;
//Resize label to the amount of text
[cell.complishLabel sizeToFit];
NSLog(@"calculated height of label: %f", cell.complishLabel.frame.size.height);
//Get height of label
float height = cell.complishLabel.bounds.size.height;
return 50 + height;
} else if (indexPath.section == 1) {
//Get a statement from the datasource and assign it to the label
NSArray *tomorrowComplishs = [[ZSSComplishStore sharedStore] tomorrowComplishs];
ZSSComplish *complish = tomorrowComplishs[indexPath.row];
cell.complishLabel.text = complish.statement;
//Resize label to the amount of text
[cell.complishLabel sizeToFit];
//Get height of label
float height = cell.complishLabel.bounds.size.height;
return 50 + height;
}
return 100;
}
这是ZSSComplishTableViewCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
答案 0 :(得分:0)
我想你什么时候
NSLog(@"calculated height of label: %f", cell.complishLabel.frame.size.height);
方法sizeToFit
无法计算complishLabel
帧。请尝试使用sizeThatFit:方法
答案 1 :(得分:0)
这是我发现的工作:
//Set a random frame
CGRect labelFrame = CGRectMake(0, 0, 263, 1000);
//make a label with that frame
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
//set a preferredMaxLayoutWidth to what you want it constrained to
label.preferredMaxLayoutWidth = 263;
//configure your label
UIFont *font = [UIFont fontWithName:@"Gill Sans" size:18];
label.font = font;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.text = complish.statement;
//Set the number of lines to 0 so that it will use as many as the width and font says it needs
label.numberOfLines = 0;
//resize the label
[label sizeToFit];
//The height will now be adjusted to the amount of text
float height = label.frame.size.height;
return 50 + height;
答案 2 :(得分:0)
计算UILabel的高度很容易,但也很棘手。有案例需要报道。我正在使用下面的代码计算我的项目的高度。这将支持iOS 6和iOS 7或更高版本。我假设您的标签中有多行。否则,您可以调整此功能以满足您的需求。
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
static CGSize sizeForTextConstrainedToSize(NSString *text, UIFont *font, CGSize maxSize)
{
if (SYSTEM_VERSION_LESS_THAN(@"7.0")) {
CGSize size = [text sizeWithFont:font constrainedToSize:maxSize];
return size;
} else {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObjects:@[font, paragraphStyle] forKeys:@[NSFontAttributeName, NSParagraphStyleAttributeName]];
CGRect newRect = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil];
CGSize sizeAlignedToPixel = CGSizeMake(ceilf(newRect.size.width), ceilf(newRect.size.height));
return sizeAlignedToPixel;
}
}
对于上面的代码,您不应该计算heightForRowAtIndexPath内的高度,而是提前计算并将其存储在变量中,以确保用户滚动tableView时没有延迟。
OR:使用estimatedHeightForRowAtIndexPath,然后你可以保持你的代码在上面,但在estimatedHeightForRowAtIndexPath中返回一个近似值。
我还注意到你使用sizeToFit来获得高度,这是不对的。无论你想得到什么样的高度,你都会有一个边界。在这种情况下,它是tableView的宽度。所以你每次想要获得一个高度时都必须提供一个最大的CGSize,这是" tableView' s宽度x MAX_INT"