gpt4 book ai didi

ios - sizewithattributes 未返回正确的大小

转载 作者:可可西里 更新时间:2023-11-01 03:09:01 26 4
gpt4 key购买 nike

我正在尝试计算 uitableview 单元格中文本标签的高度。在看到 sizewithfont 在 ios 7 中被弃用后,我实现了 sizewithattributes,但返回值远小于标签所包含的文本大小正确的值。我也试过 sizetofit 方法也无济于事。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *message = self.messages[indexPath.row];

UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1];
UILabel *messageContent = (UILabel *)[cell.contentView viewWithTag:3];
UIImageView *image = (UIImageView *)[cell.contentView viewWithTag:2];
messageContent.text = [message objectForKey:@"messageContent"];
NSString *content = [message objectForKey:@"messageContent"];
NSLog(@"Message: %@", content);

CGSize textSize = [content sizeWithAttributes:@{ NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]}];
messageContent.font = [UIFont fontWithName:@"HelveticaNue-Light" size:17.0];
CGRect messageFrame = messageContent.frame;
messageFrame.size = textSize;
messageContent.frame = messageFrame;


nameLabel.text = [message objectForKey:@"senderName"];
NSString *senderPicture = [message objectForKey:@"senderPicture"];
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString: senderPicture]]];

image.image = myImage;
image.layer.cornerRadius = 27.0;
image.layer.masksToBounds = YES;

//Configure the cell...

return cell;
}

最佳答案

归因:

user/Elio.d有一个很好的答案here.

我在下面附上了他的回答的抄本。如果对你有帮助请务必去发Elio.dhis original answer 点赞


成绩单:

你可以试试这个:

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:14.0f]};
// NSString class method: boundingRectWithSize:options:attributes:context is
// available only on ios7.0 sdk.
CGRect rect = [textToMeasure boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];

关于ios - sizewithattributes 未返回正确的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20260384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com