我试图在运行时动态设置我的UITableViewCell内部标签的高度。标签的高度取决于内容。我在我的自定义UITableViewCell xib文件中设置了UILabel应该有0行(没有边界)。下面是我的CellForRowAtIndexPath代码。我环顾四周,尝试了一些东西,但似乎没有什么对我有用。
XYZPlayerTableViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = indexPath.row;
static NSString *CellIdentifier = @"PlayerCell";
XYZPlayerTableViewCell *cell = (XYZPlayerTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
XYZPlayer *player = [self.players objectAtIndex:row];
NSString *nameText = player.name;
NSString *gameText = player.game;
cell.nameLabel.text = nameText;
cell.gameLabel.text = gameText;
[cell.gameLabel sizeToFit];
cell.gameLabel.numberOfLines = 0;
cell.ratingImageView.image = [self imageForRating:player.rating];
CGSize boundingRect = CGSizeMake(cell.frame.size.width, 4000);
CGRect expectedFrame = [cell.gameLabel.text boundingRectWithSize:boundingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: cell.gameLabel.font} context:nil];
cell.gameLabel.frame = expectedFrame;
NSLog(@"gameLabel = 0x%@ gameText = %@ gameLabelHeight = %.2f", cell.gameLabel, gameText, expectedFrame.size.height);
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int row = indexPath.row;
//set width depending on device orientation
self.cellPrototype.frame = CGRectMake(self.cellPrototype.frame.origin.x, self.cellPrototype.frame.origin.y, tableView.frame.size.width, self.cellPrototype.frame.size.height);
// Get variables
XYZPlayer *player = [self.players objectAtIndex:row];
UILabel *nameLabel = self.cellPrototype.nameLabel;
NSString *nameText = player.name;
UILabel *gameLabel = self.cellPrototype.gameLabel;
NSString *gameText = player.game;
// Calculate height
CGFloat nameLabelHeight = [self sizeOfLabel:nameLabel withText:nameText].height;
CGFloat gameLabelHeight = [self sizeOfLabel:gameLabel withText:gameText].height;
CGFloat padding = nameLabel.frame.origin.y;
CGFloat combinedHeight = padding + nameLabelHeight + padding/2 + gameLabelHeight + padding;
return combinedHeight;
}
XYZPlayerTableViewCell.m
-(void)setGameLabelHeightForString:(NSString *)string
{
self.gameLabel.numberOfLines = 0;
gameLabel.text = string;
CGSize maximumLabelSize = CGSizeMake(gameLabel.frame.size.width, FLT_MAX);
CGSize expectedLabelSize = [string sizeWithFont:self.gameLabel.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];
CGRect newFrame = self.gameLabel.frame;
newFrame.size.height = expectedLabelSize.height;
self.gameLabel.frame = newFrame;
}
-(void) layoutSubviews
{
[self setGameLabelHeightForString:gameLabel.text];
}
运行时的输出是:
这就是我的应用程序的样子。灰色显示标签的高度
任何人都对我做错了什么有任何想法?
答案 0 :(得分:0)
您可以使用它来计算身高,但这不是推荐的方式
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
XYZPlayerTableViewCell *cell=(XYZPlayerTableViewCell *)[self tableView:tblView cellForRowAtIndexPath:indexPath];
return CGRectGetMaxX(cell.gameLabel.frame)+5;
}
答案 1 :(得分:0)
再使用一行代码
cell.gameLabel.numberOfLines = 0;
答案 2 :(得分:0)
要根据您的内容调整标签大小,您必须确保2件事
行数= 0 并在您的标签上调用 sizeToFit 。
不确定您是否设置了线条。尝试仅在委托内设置行,否则看起来没问题
UILabel *tempLabel = [[UILabel alloc]initWithFrmae:_textView.bounds];
tempLabel.numberOfLines = 0; // Important to do
tempLabel.text = @"Your Text";
[tempLabel sizeToFit]; // This will resize your Label and now you can use it's height to manage your textView.
这会根据它的文字调整tempLabel的大小。 尝试仅在委托内设置numberOfLines。 还要尝试根据您的标签给出单元格的高度,方法是在 heightForRowAtIndexPath 委托
中计算它们答案 3 :(得分:0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
float calulatedWidth = [self getWidthFromString:textTobePutInLabel];
float calculatedHeight = [self getHeightFromString:textTobePutInLabel];
cell.gameLabel.frame = CGRectMake(yourX,yourY,calulatedWidth,calculatedHeight)
cell.gameLabel.text = gameText;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
float height = [self getHeightFromString:textTobePutInLabel];
return height;
}
- (float)getWidthFromString:(NSString *)strName{
CGSize maximumSize = CGSizeMake(9999, 9999);
CGSize size = [strName boundingRectWithSize:maximumSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:MAVEN_PRO size:13.0f]} context:nil].size;
return size.width;
}
- (float)getHeightFromString:(NSString *)strName{
CGSize maximumSize = CGSizeMake(9999, 9999);
CGSize size = [strName boundingRectWithSize:maximumSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:MAVEN_PRO size:13.0f]} context:nil].size;
return size.height;
}
答案 4 :(得分:0)
你需要做两件事
覆盖heightForRowAtIndexPath
UITableViewDelegate
以根据文字动态返回单元格的高度。
覆盖layoutSubviews
UITableViewCell
(XYZPlayerTableViewCell)重新计算并设置标签框架。
你可以使用
CGSize labelSize = [self.gameLabel.text sizeWithFont:[UIFont systemFontOfSize:11]
constrainedToSize:CGSizeMake(300, NOTIFICATION_DESC_MAX_HEIGHT + 5)
lineBreakMode:NSLineBreakByWordWrapping];
如果要将String设置为label或
,则计算最佳拟合高度CGRect rect = [self.gameLabel.attributedText boundingRectWithSize:CGSizeMake(300, NOTIFICATION_DESC_MAX_HEIGHT + 5)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
context:nil];
如果您使用AttributedString作为标签
- 维沙尔
答案 5 :(得分:0)
我检查了你的代码,我注意到在设置标签的框架后放置了[gameLabel sizeToFit];
语句的一个问题。
即,在这些行之后
CGSize boundingRect = CGSizeMake(cell.frame.size.width, 4000);
CGRect expectedFrame = [cell.gameLabel.text boundingRectWithSize:boundingRect options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: cell.gameLabel.font} context:nil];
cell.gameLabel.frame = expectedFrame;