0

I'm currently working on a social media app just to try and excel my Swift skills. I'm using the UIKit framework to build this app. I've got the feed up and running, and scaling pictures correctly there went fine because I can calculate the height of an image if I know its width and aspect ratio. Now that I've starting working on the comment section, I can't solve the problem this way. I need to find a way to know what height a cell is (the comment section is a UITableView). The problem is that to use the result from label.frame.height, I must have set its constraints. But when setting constraints, I don't know what the height of the cell is, so it's sort of a circular problem: I don't know the height -> I can't set constraints -> I don't know the height -> ...

What I've tried is calling this function when initializing my custom UITableViewCell (which holds the comment and the person who posted it, as well as the number of likes it has):

    contentView.addSubview(label)
    label.font = UIFont(name: "Arial", size: 14)
    label.lineBreakMode = .byWordWrapping
    label.adjustsFontSizeToFitWidth = false
    label.numberOfLines = 0
    label.translatesAutoresizingMaskIntoConstraints = false
    label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: padding).isActive = true
    label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: padding).isActive = true
    label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -padding).isActive = true
    label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -padding).isActive = true

And my heightForRowAt indexPath function looks like this:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let cell = CommentCell(style: .default, reuseIdentifier: Cells.commentCell)
    cell.setText(to: comments[indexPath.row].text)
    return cell.fetchCellHeight(with: cell.content)
}

where cell.fetchCellHeight(with:) is defined as follows:

    label.text = text
    return label.frame.height

So what I've tried is setting the text and trying to read the label's height, but for some reason this returns 0.0.

So in short, what I'm wondering is how I can find the label's height so that the cells in my UITableView get the correct height depending on how much text is in them. I'm doing this all programmatically, so unfortunately any Storyboard solution won't work for this problem unless there is a programmatic equivalent.

Thank you.

Frævik
  • 7
  • 3
  • `yourLabel.font.lineHeight` will give you the height of the label if it's a single line. Is your label multiple lines? Also, do you know you can make each cell dynamic based on it's height without using something like `cell.fetchCellHeight` – Waylan Sands Jul 25 '20 at 23:32
  • These labels would be an unknown number of lines. It's all based on how much text a user comments. If a cell is dynamic, does that mean I can change its height on runtime, and how could I go about doing this? – Frævik Jul 25 '20 at 23:51
  • Why are you changing the height? – Waylan Sands Jul 26 '20 at 00:05
  • I just need a way to actually find the height of an arbitrary label. If I do that, I don't need to change its height at runtime. The problem is that I don't know exactly how I can find its height. – Frævik Jul 26 '20 at 01:17
  • Does this answer your question? [Adjust UILabel height to text](https://stackoverflow.com/questions/25180443/adjust-uilabel-height-to-text) – Magnas Jul 26 '20 at 05:00
  • So your problem is to estimate the height of label ? Did you see this thread: https://stackoverflow.com/questions/30450434/figure-out-size-of-uilabel-based-on-string-in-swift – claude31 Jul 26 '20 at 07:16

0 Answers0