0

I have a UITableViewCell which is used as self resizing tableViewCell. It has one UILabel, one UIView and one UIButton inside it. All of them are put vertically inside. I want UILabel and UIView to be resized automatically when needed. They are getting resized as expected. But the problem is, I cannot get their height after resizing. frame.size.height always show the previous height.

Here is an image of the xib file: enter image description here

4

3 回答 3

0

我不确定您的自动布局如何控制您的标签高度,但也许您可以依靠根据它将显示的文本和您已经使用的已知约束(例如 373 宽度)来确定标签的高度

CGFloat maxWidth = 373, maxHeight;  // Whatever you desire here, 373 from your example

CGFloat messageHeight = ceilf([detailsString boundingRectWithSize:CGSizeMake(maxWidth, maxHeight)
                                                           options:<ANY_OPTIONS_HERE>
                                                        attributes:@{NSFontAttributeName:detailTextLabel.font}
                                                           context:nil].size.height);
于 2017-10-22T19:29:50.423 回答
0

我通过这样做获得了高度。的高度cell.detailsTextLabel是所需的高度。

UILabel *label = [[UILabel alloc] init];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.preferredMaxLayoutWidth = 373;

[label invalidateIntrinsicContentSize];
label.text = detailsString;
label.font= cell.detailTextLabel.font;

CGFloat theSweetLittleHeight = label.intrinsicContentSize.height;
于 2017-10-22T18:56:18.517 回答
-1

您可以为标签和视图的高度约束创建一个出口,如下所示,并在 tableView 加载后从中获取值。

 @IBOutlet weak var labelHeightConstraint: NSLayoutConstraint!
 @IBOutlet weak var viewHeightConstraint: NSLayoutConstraint!
 let labelHeight = labelHeightConstraint.constant
 let viewHeight = viewHeightConstraint.constant
于 2017-10-22T13:55:06.013 回答