2016-10-25 12 views
0

- (CGFloat)のtableView:(のUITableView *)のtableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {テーブルビューセルIOSでlabetテキストにセルの高さを拡張

NSString * message =[[chatData objectAtIndex:indexPath.row]valueForKey:@"message"]; 
NSString * message_type = [[chatData objectAtIndex:indexPath.row]valueForKey:@"message_type"]; 

if([[[chatData objectAtIndex:indexPath.row]valueForKey:@"message_type"] isEqualToString:@"0"]){ 
    CGSize sizeText = [self text:message sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds) - 100, 9999) withLinebreakmode:NSLineBreakByWordWrapping]; 
    CGFloat heightOfRow = sizeText.height + 2 * 7.5f; 
    return heightOfRow + 50; 
} 
else 
    return 225;//message_type 1 3 4 

// return self.chatTable.rowHeight; 

}

プラグママーク - ChatLabel幅の取り扱い

- (CGSize)テキスト:(NSStringの*)テキストsizeWithFont:(UIFont *)フォントconstrainedToSize:(CGSize)ConstSize withLinebreakmode:(NSLineBreakMode)linebreakmode {CGSizeのretsize。

if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) 
{ 
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
    paragraphStyle.lineBreakMode = linebreakmode; 
    // paragraphStyle.alignment = NSTextAlignmentLeft; 
    NSDictionary * attributes = @{NSFontAttributeName : font,NSParagraphStyleAttributeName : paragraphStyle}; 
    retsize = [text boundingRectWithSize:ConstSize options:NSStringDrawingUsesFontLeading 
       |NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; 
    retsize.height =ceilf(retsize.height); 
    retsize.width =ceilf(retsize.width); 

    NSLog(@"%f",retsize.width); 


}else{ 

    if ([text respondsToSelector:@selector(sizeWithFont:constrainedToSize:lineBreakMode:)]){ 
     retsize = [text sizeWithFont:font 
        constrainedToSize:ConstSize 
         lineBreakMode:linebreakmode]; 
     retsize = [text boundingRectWithSize:ConstSize options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil].size; 
    } 
} 
//[self viewWillAppear:YES]; 
return retsize; 

}

答えて

0

このコードを試してみてください。

1.このコードを使用する場合は、ラベルの上、下、左、右の拘束条件のみを指定する必要があります。

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //Here you give a minimum height of the cell size. 
    return 110; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewAutomaticDimension; 
} 
関連する問題