Textlabel automatic line wrapping in uitableview cell

Textlabel automatic line wrapping in uitableview cell

http://blog.csdn.net/u010886174/article/details/17960061

After reading a few articles, I finally got it.

1. Only these two lines of code are not enough, because the height of the cell has not changed, although it can wrap, but the space is not enough, or the display is incomplete. If you don’t believe me, try it.

cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;

 

2. Add the following code:

 

/**
 * cell height
 *
 *  @param tableView <#tableView description#>
 *  @param indexPath <#indexPath description#>
 *
 *  @return <#return value description#>
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = self.qrCode.QR_DATA; //The string you want to display
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
    CGSize size = [str boundingRectWithSize:CGSizeMake(280, 999) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return size.height + 30;
}
/**
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *str = self.qrCode.QR_DATA; //The string you want to display
    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Helvetica" size:17]constrainedToSize:CGSizeMake(280, 999) lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"%f",size.height);
    return size.height + 10;
}
 */

 

3, gone.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326641760&siteId=291194637