• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - sizewithAttributes 没有为 uilabels 返回正确的高度

[复制链接]
菜鸟教程小白 发表于 2022-12-12 17:30:34 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试使用 sizewithattributes 方法计算 uitableview 中 uilabels 的正确高度。但是它返回的高度远低于应有的高度,标签文本被截断,并且从 textwithattributes 返回的高度输出到控制台显示的高度约为 20 - 40,即使对于整段文本也是如此。我已将我的字体大小和字体设置为与方法的属性部分中的 uilabel 相同,并且我将换行模式设置为 uilabel 的自动换行。提前致谢

代码:`

- (UITableViewCell *)tableViewUITableView *)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: boundingRectWithSizeptions: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;
}
`



Best Answer-推荐答案


你的问题是:

//[content boundingRectWithSize:CGSizeMake(10000, MAXFLOAT)

您指定 100000 pt 作为文本的宽度,我不知道您的 content 字符串包含多少个字符,但我相信您可以轻松地将其显示在一行或两行中是 10000 pt 长。

更好的方法是将文本添加到它的标签读取大小并在需要时更改标签框架:

CGSize labelSize = [self.messageContent.text sizeWithAttributes{NSFontAttributeName:[UIFont systemFontOfSize:self.messageContent.font.pointSize]}];

关于ios - sizewithAttributes 没有为 uilabels 返回正确的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20764778/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap