Я пытаюсь вычислить правильную высоту uilabels в uitableview с помощью метода sizewithattributes. Но высота, которую он возвращает, значительно ниже того, что она должна быть, текст меток обрезается и выводит высоту, возвращаемую с textwithattributes на консоль, показывает высоту около 20 - 40 даже для полных абзацев текста. Я установил размер шрифта и шрифт таким же, как uilabel в разделе атрибутов метода, и у меня есть режим прерывания строки, установленный для wordwrap для uilabel. Спасибо заранееsizewithAttributes не возвращает правильную высоту для uilabels
Код: `
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *message = self.messages[indexPath.row];
NSLog(@"%lu", (long)indexPath.row);
UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:20000];
UILabel *messageContent = (UILabel *)[cell.contentView viewWithTag:10000];
UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:30000];
UIButton *replyButton = (UIButton *)[cell.contentView viewWithTag:40000];
replyButton.tag = indexPath.row;
messageContent.text = [message objectForKey:@"messageContent"];
NSString *content = [message objectForKey:@"messageContent"];
NSLog(@"Message: %@", content);
NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:17.0f]};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
CGRect rect = [content boundingRectWithSize:CGSizeMake(10000, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGFloat height = ceilf(rect.size.height);
CGFloat width = ceilf(rect.size.width);
messageContent.frame = CGRectMake(40, 126, width, height);
NSLog(@"Height:%f", messageContent.frame.size.height);
NSLog(@"y: %f", messageContent.frame.origin.y);
NSLog(@"estimated height: %f", height);
nameLabel.text = [message objectForKey:@"senderName"];
UIImage *senderPic = self.senderPictures[indexPath.row];
image.image = senderPic;
image.layer.cornerRadius = 27.0;
image.layer.masksToBounds = YES;
return cell;
}
`
это еще не решает проблему :(, она по-прежнему возвращает то же значение как и раньше – user2489946
Попробуйте sizeWithAttributes:? – Greg
Да, я поставил именно то, что вы написали выше, но проблема осталась прежней. – user2489946