如何在iphone中增加行高?

如何在iphone中增加行高?,iphone,ios6,Iphone,Ios6,我正在使用xib创建自定义单元格。我想根据标签文本显示标签。如何增加行高度和标签高度 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; customCell *cell=(customCell *)[tableView dequeueRe

我正在使用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数组不是静态的。它是动态的

我已经使用了上面的代码。

动态

- (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


您可以通过两种方式完成此操作

一,

二,

使用此选项计算标签所需的大小,然后为该标签设置新框架

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    CGSize textSize = [myText sizeWithFont: LableFont
                           constrainedToSize:CGSizeMake(LABLE_WIDTH, CGFLOAT_MAX)
                               lineBreakMode:UILineBreakModeWordWrap];
return textsize.height+somefixvalue;
}

要获得正确的高度,请使用NSString方法-boundingRectWithSize:options:attributes:context:

在两个位置使用它:CellforRowatineXpath和HeightforRowatineXpath

在CellForRowatineXpath中,然后将标签的帧高度设置为从此indexPath的特定文本上运行的方法检索到的高度


在heightForRowAtIndexPath中,使用相同的方法,使用在该indexPath的特定文本上运行的相同方法设置高度。

我感谢大家,但有一件事在IOS 7中不推荐使用。对于iOS7,只需执行以下操作

-(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]; 
}
希望对大家都有帮助。

试试这些

-(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;
}
在CellForRowatineXpath中也使用相同的方法

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;

用LBL消息替换标签名称。你已经完成了。

你在heightForCell中写了什么?如果它>=iso7引用了这个–tableView:estimatedHeightForRowAtIndexPath::constrainedToSize不推荐使用,你应该使用:boundingRectWithSize:选项:属性:上下文:他要求动态高度。
-(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]; 
}
-(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;
}
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;