我正在使用xib来创建自定义单元格。我想显示标签,具体取决于标签文字。如何增加行高和标签高度?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
customCell *cell=(customCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[NSBundle mainBundle]loadNibNamed:@"customCell" owner:self options:nil]objectAtIndex:0];
UIImage *userimage=[self getUserImage:[userimageURLS objectAtIndex:indexPath.row]];
cell.userImageViewIv.autoresizesSubviews=YES;
cell.userImageViewIv.image=userimage;
cell.userNameLbl.text=[UserName objectAtIndex:indexPath.row];
cell.commenttxtlbl.text=[postText objectAtIndex:indexPath.row];
NSLog(@"cell created");
}
return cell;
}
变量posttext数组不是静态的。这是动态的。
我使用了上面的代码。
答案 0 :(得分:1)
CGSize textSize = [myText sizeWithFont: LableFont
constrainedToSize:CGSizeMake(LABLE_WIDTH, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
使用它来计算标签所需的尺寸,然后为该标签设置新框架。
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize textSize = [myText sizeWithFont: LableFont
constrainedToSize:CGSizeMake(LABLE_WIDTH, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
return textsize.height+somefixvalue;
}
答案 1 :(得分:0)
动态
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *attribValue = stringToBeWrittenInTheCell;
CGFloat labelWidth = WidthOfTheCellLabel;
titlelblSize = [attribValue
sizeWithFont:[UIFont
boldSystemFontOfSize: FontSize
constrainedToSize:CGSizeMake(labelWidth,labelHeight)
lineBreakMode:UILineBreakModeWordWrap];
[attribValue release];
attribValue = nil;
return titlelblSize.height;
}
静态
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 71.0;
}
或使用storyboard / xiv
答案 2 :(得分:0)
你可以用两种方式做到这一点......
1
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
2
[tableview setRowHeight:80];
答案 3 :(得分:0)
要获得正确的高度,请使用NSString方法-boundingRectWithSize:options:attributes:context:
在2个地方使用它:cellForRowAtIndexPath和heightForRowAtIndexPath。
在cellForRowAtIndexPath中,将标签的帧高度设置为从此方法检索的高度,该高度是针对此indexPath的特定文本运行的。
在heightForRowAtIndexPath中,使用相同的方法,使用在此indexPath的特定文本上运行的相同方法设置高度。
答案 4 :(得分:0)
我很感谢你们所有人,但有一件事sizeWithFont
现在已经在IOS 7中弃用了。对于IOS 7,只需要执行以下操作
-(CGRect)sizeOfText:(NSString *)str
{
return [str boundingRectWithSize:CGSizeMake(LABEL_WIDTH, LABEL_MAX_HEIGHT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"YourLabelFontName" size:16.0f]} context:Nil]; //instead of 16.0f put your Label font size
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// include the below code with yours
CGRect stringRect=[self sizeOfText:(NSString *)[postText objectAtIndex:[indexPath row]]];
float labelHeight=stringRect.size.height;
return labelHeight + 100.0f; // here 100.0f is default change it according to your cell height
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// include the below code with yours
NSString *commentString=[postText objectAtIndex:indexPath.row];
CGRect stringRect=[self sizeOfText:commentString];
cell.commenttxtlbl.text=commentString;
CGRect newRect=CGRectMake([cell.commenttxtlbl frame].origin.x, [cell.commenttxtlbl frame].origin.x, stringRect.size.width, stringRect.size.height);
[cell.commenttxtlbl setFrame:newRect];
}
希望它对你们所有人都有帮助..
答案 5 :(得分:0)
试试这些
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
MessageConversation *message = [marrMessage objectAtIndex:indexPath.row];
CGSize szMaxCell = CGSizeMake(220, 2999);
UIFont *font = [UIFont systemFontOfSize:14.0f]; // whatever font you're using to display
CGSize szCell = [message.message sizeWithFont:font constrainedToSize:szMaxCell lineBreakMode:NSLineBreakByWordWrapping];
// NSLog(@"szCell %@",NSStringFromCGSize(szCell));
if(szCell.height > 120){
return szCell.height+100;
}
return 150;
}
也在CellforRowAtIndexpath中使用相同的
rightCell.lblMessage.numberOfLines = 0;
CGSize szMaxCell = CGSizeMake(220, 2999);
UIFont *font = [UIFont systemFontOfSize:17.0f]; // whatever font you're using to display
CGSize szCell = [message.message sizeWithFont:font constrainedToSize:szMaxCell lineBreakMode:NSLineBreakByCharWrapping];
CGRect lblFrame = rightCell.lblMessage.frame;
lblFrame.size.height = szCell.height;
rightCell.lblMessage.frame = lblFrame;
用lblMessage替换您的标签名称。你完成了。