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

Ashik
  • 1,204
  • 12
  • 22
  • May I ask when you're fetching `frame.size.height`? Is it in `layoutSubviews`? – EDUsta Oct 22 '17 at 15:38
  • No, Im calling it inside cellForRowAtIndexPath. For previous 10 hours :( . Appreciate any help mate. – Ashik Oct 22 '17 at 15:52
  • Actually I got the height of UIView by calling [theView layoutIfNeeded]. But the UILabel is so damn crazy, it returns the default height from xib file. – Ashik Oct 22 '17 at 15:57
  • I've tested in on my project (I have a cell with only one label, which can grow as far as it can go). According to the results, without calling `layoutIfNeeded`, it returns default height. With calling `layoutIfNeeded`, it returns a bit more (previous value was 27.5, now it's 33.5), but not the correct one. `willDisplayCell` isn't also working, but `layoutSubviews` in customCell works after the first call. The first call is also 27.5. Hope this helps. – EDUsta Oct 22 '17 at 16:18
  • So... @EDUsta, I found a way. Thank you very much for your help. I answered my own question. See the answer. – Ashik Oct 22 '17 at 18:57

3 Answers3

0

I got the height by doing this. Height of cell.detailsTextLabel is the desired height.

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;
Ashik
  • 1,204
  • 12
  • 22
0

I am not sure how your auto layout is controlling your labels height, but perhaps you can rely on determining the height of the label based on the text it will display and known constraints you have already been using (such as the 373 width)

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);
Will Von Ullrich
  • 2,129
  • 2
  • 15
  • 42
  • Thanks for the answer man. I tried that. Didn't help much. :) – Ashik Oct 22 '17 at 21:05
  • Did it give you the wrong height or just not useful? – Will Von Ullrich Oct 22 '17 at 21:44
  • Make sure you specify `options:NSStringDrawingUsesLineFragmentOrigin`. Any time I have used this approach, as long as I pass the correct font (font name, size, and weight) and the maximum `CGSize`, it gives me the exact height that it adjusts to from calling layoutIfNeeded or layoutSubviews. Also if you have any constraints controlling the height outside of fitting the text, account for it statically after fetching the `messageHeight` – Will Von Ullrich Oct 23 '17 at 18:53
-1

You can create an outlet for the height constraint for the label and view as below and get the value from them after tableView loads.

 @IBOutlet weak var labelHeightConstraint: NSLayoutConstraint!
 @IBOutlet weak var viewHeightConstraint: NSLayoutConstraint!
 let labelHeight = labelHeightConstraint.constant
 let viewHeight = viewHeightConstraint.constant
Swathi
  • 115
  • 2