无法根据objective-c中标签的文本大小设置单元格大小 [英] Unable to set cell size according to label's text size in objective-c

查看:18
本文介绍了无法根据objective-c中标签的文本大小设置单元格大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试聊天应用.聊天工作正常.面临一些 UI 问题.

I am trying for chat app. Chat working fine. Facing some UI issues.

1. 我正在为标签使用自动布局.所以根据消息大小自动标签获取扩展.但是,我在 UITableViewCell 中有标签.我无法动态计算行高.

1. I am using auto layout for label. So automatically label get expand according to the message size. But, I am having label in UITableViewCell. I couldn't calculate row height dynamically.

我的代码:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGSize textSize = {268, CGFLOAT_MAX};
CGSize size = [message sizeWithFont:[UIFont fontWithName:@"Helvetica Neue" size:14] constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = size.height  < 65 ? 65 : size.height;
return height;
}

我的标签子类代码

- (void)drawTextInRect:(CGRect)rect
{
    self.textInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.textInsets)];
}

- (CGSize) intrinsicContentSize
{
    self.textInsets = UIEdgeInsetsMake(5, 5, 5, 5);
    CGSize superSize = [super intrinsicContentSize] ;
    superSize.height += self.textInsets.top + self.textInsets.bottom ;
    superSize.width += self.textInsets.left + self.textInsets.right ;
    return superSize ;
}

以上代码适用于小消息,例如 400 到 800 个字符.更重要的是它不起作用.根据标签大小,单元格大小没有增加.请指导我.

The above code working for small messages, like 400 to 800 characters. More than that it is not working. Cell size not getting increase, as per label size. Kindly guide me.

我的输出:

在上面的截图中,显示时间的小消息和完整的消息.如果我们输入大文本,20% 的消息会被隐藏.请指导我.如何解决这个问题?

In the above screenshot, small messages displaying time and full message. If we type large text, 20% of message getting hide. kindly guide me. How to solve this?

推荐答案

为您的标签使用 Storyboard 并仅使用单个标签 将您的标签设置为具有所有 4 个约束,左、右、上、下 设置行数 0.在您的 ViewController 中使用这 2 种方法,表格视图将根据您的文本自动设置高度

Use Storyboard for your label and use only single label Set your label to have all 4 constraints, left,right,top,bottom Set number of lines 0. In your ViewController use these 2 methods,Table view will automatically set height depend on your text

 -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 return UITableViewAutomaticDimension;
}

这篇关于无法根据objective-c中标签的文本大小设置单元格大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆