How to auto adjust cell’s heights depending their text inside? - ios

I am using a tableview to display a list of articles. Each cell needs to show an image and a text that has a brief descriptions of articles. What I want is to adjust the cells height depending the description length.
I know that I can resize the cells with the delegate method heightForRowAtIndexPath but I still don’t know which height to return.
Any help?

Ok, you can do that just using Interface builder. On the Tableview properties click “Row height” automatic.

Then you can use a UILabel with property lines set to 0 to display your text. When adding the constraints to the cell you need to make sure that the height of cell depends on the intrinsic content size of the label, basically is the size of the content (the text inside the label).
For example:

Here I have added a UIImage with constant height and width and with top and leading space to 0. Also I added a UILabel with top/bottom/leading/trailing space to 0. Here the cell height will depend on the UILabel intrinsic size that is what you want. Also to prevent glitches with short texts you can add a minimum height size to the UILabel.
Of course this is an example, you can achieve the same using other constraints. The main thing here is to be aware of the concept of intrinsic content size: a predetermined size of a view based in its content.

Let's say you have a textView in each cell. The width of the textView is fixed. And you know the text for each cell. Base on the info above, you can get the height of your textView by:
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.greatestFiniteMagnitude))
Then you should be able to calculate the height of your cells base on your layout and stuff.

The question is answered but just in case you want to know... You can return UITableViewAutomaticDimension in heightForRowAtIndexPath.
Nevertheless you should google more before asking a question that has been answered multiple times on the web (and on stackoverflow) ;)

Related

UITableView automatic dimension with UITextView max size of cell

I've successfully created UITableView with custom cells (automatically sized) that contain a label and UITextView - detailsTextView.
When scrolling of detailsTextView is disabled, the cells are properly resized according to textView's text.
I don't want, however, to have extremely large cells and set maximum height of a cell (and enable scrolling for detailsTextView when it reaches max cell height).
How can I achieve this?
When enabling scrolling for detailsTextView autoresizing of cells shrinks it to 0 height (overriden by min. height constraint in IB), but still it does not fill my planned maximum size of cell.
override func viewDidLoad() {
mainTableView.rowHeight = UITableViewAutomaticDimension
mainTableView.estimatedRowHeight = 200
self.mainTableView.register(UINib.init(nibName: EventCell.nibName,
bundle: Bundle.main),
forCellReuseIdentifier: EventCell.cellIdentifier)
...
}
The idea is simple. You need to keep track of UITextView's height change in textViewDidChange method.
Then if UITextView exceeds your maximum predefined height then you are gonna add a constraint which restrict the UITextView's height growth. Constraint should be something like setting minimum height of UITextView to your maximum predefined height and enable the scroll for UITextView.
If you use only auto layout, UITextView can define it's own size only if scrolling is disabled, unfortunately.
You should try to avoid recursive layout passes (e.g. when text view's height changes, then install more constraints, this will cause the height to change again, etc, etc).
What you can do is limit maximumNumberOfLines of your textView, and provide other ways to see the full text: as it was pointed out, it is a very bad UX practice to have a scroll view within another scroll view.
From a UI/UX standpoint I wouldn't do that what you are trying to do.
Having a Scrollview (UITableview) with another scrollview in it (UITextview) is bad. It can confuse the user because he could scroll the "textbox" on smaller devices instead of the "table".
However, you could go with this solution: UITextView change height instead of scroll
Another solution would be to check what happens when the cell with 0 height is shown. Check viewWillLayoutSubviews() and the constrains in IB.
Hope this gets you one step closer to the solution/answer.

Scrollview doesn't scroll with multiline label in it

I have a multiline label inside a scrollview. I set up the content size, let's say to scrollView.contentSize.height = 2000
But the view doesn't scroll. There is barely any code in the project. What is going wrong?
The only thing is that I don't have constrain for the height of the label, because it will vary depending on the length of text.
It doesn't matter about the height. But what does matter is that you need to pin it to the bottom of the scroll view also.
By pinning it on the top and bottom it will use the label to set the content size and so allow it to scroll.
I suggest to add a UITableView instead of UIScrollView, adding one UITableViewcell that contains a UILabel. By setting the appropriate values of:
Label's constraints.
tableView's rowHeight.
tableView's estimatedRowHeight.
It should works fine for your case.
For more Information about setting a dynamic cell height, you might want to check this answer.
Hope that helped.

How to create UITableViewCell with dynamic cell height based on its content

I was trying to explore the new feature of dynamic cell height using auto layout, introduced in Xcode 7 using this link. I have one UITableViewCell like this below
I want cell to adjust its size automatically based on the content in textView and size of image. I set all constraint and given estimatedRowHeight and UITableViewAutomaticDimension as tableview row height. But when i run the app i'm not able to see the UITextView below. Which means that cell height is not getting adjusted dynamically. Do i need to set the cell height programmatically or still i can do it using Auto Layout.
For TextView, it must not be scrollable.
Also, Your constraints are should be provided in such a way that, TableViewCell should get height automatically(there must be vertical spacing constraints between each component, Height of the each component must be there(it may be explicit of implicit height, but it must need to have height)).
Also, estimatedHeight you are providing must be near to actual average height.
If your tableViewCell is getting above things, then and then only it will get dynamic height based on its content.

Creating different cell sizes with dynamic height and full height

I understand how to create custom UITableViewCells with a dynamic height in iOS8+.
Within our app we have some full size height cells, for error/loading states. Then we have some dynamic height cells for the actual correct content.
What is the correct way to handle a full height cell using the dynamic height approach? Previously we would use the heightForRowAtIndex and set the height to the be the tableView.frame.size.height for example.
Now we need to have some cells which would be fullsize, some which might be 200 and some which might be 240 as an example.
Just setup the constraints in your full size cells to calculate to the correct height. You can either give them a constant height constraint or base it on it's content.
That way you don't have to mix absolute and dynamic heights.

How to calculate height of a tableviewcell with right/left detail style?

I have a UITableView with prototype cells in right/left detail style. The text length of both (multiline) labels varies pretty much.
This means that the width of the labels will change in regard to the text (even within the same section).
In a more general sense, this question applies to all cells containing at least 2 labels which are aligned horizontally to each other.
Is it possible to calculate the resulting cell height (in heightForRowAtIndexPath) without a given constraining label width?
I would like to layout the text in as few lines as possible = minimizing the cell height (see picture).
Any suggestions/solutions are appreciated!
The height for the row is calculated in tableview delegate's method - tableView:heightForRowAtIndexPath:
This is where you have to calculate the possible width & height of labels. It is possible to do by using [#"LOOOONG TEXT" sizeWithFont:defaultFont]; Then just do the appropriate math and return needed height for the cell.

Resources