У меня есть UITableView, который заполняется текстом и изображениями из файла JSON. В настоящее время ячейка TableView корректно правильна для "сообщений", которые не содержат много разрывов строк в тексте, однако я не могу вычислить правильную высоту для "сообщений" с 4 или 5 разрывами строк.
Код для получения высоты:
-(float)height :(NSMutableAttributedString*)string
{
NSString *stringToSize = [NSString stringWithFormat:@"%@", string];
CGSize constraint = CGSizeMake(LABEL_WIDTH - (LABEL_MARGIN *2), 2000.f);
CGSize size = [stringToSize sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:contraint lineBreakMode:NSLineBreakByWordWrapping];
return size.height;
}
Как рассчитать правильный размер, разрешая разрывы строк и пробелы?
ИЗМЕНИТЬ
Остальная часть метода,
Внутри TableView CellForRow:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *row = [NSString stringWithFormat:@"%i", indexPath.row];
float postTextHeight = [self height:postText];
NSString *height = [NSString stringWithFormat:@"%f", heightOfPostText + 70];
[_cellSizes setObject:height forKey:row];
}
И высота таблицы:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *imageHeightString = [NSString stringWithFormat:@"%@", [_cellSizes objectForKey:indexPath.row]];
float heightOfCell = [imageHeightString floatValue];
if (heightOfCell == 0) {
return 217;
};
return heightOfCell + 5;
}