Problem definition
I am trying to build a custom control which will behave similarly to UILabel
. I should be able to place such a control inside of a self-sizing table cell and it should:
- Wrap it's content (like UILabel with numberOfLines=0 does)
- automatically extend self-sized cell height size
- handle a device rotation
- don't require any special code in UITableCellView or ViewControll to implement this functionality (UILabel doesn't require any special code for that).
Research
The first thing which I did is very simple. I decided to observe how UILabel works. I did following:
- created a table with self-sizing cells
- created a custom cell, put UILabel (with
numberOfLines=0)
in it - created constraints to make sure that UILabel occupies a whole cell
- subclasses UILabel and overrode a bunch of methods to see how it behaves
I checked following things
- Run it in a portrait (the label is displayed correctly over several lines) and the cell height is correct
- Rotate it. The table width and height was updated and they are correct too.
I observed that it behaves well. It doesn't require any special code and I saw the order of (some) calls which system does to render it.
A partial solution
@Wingzero wrote a partial solution below. It creates cells of a correct size.
However, his solution has two problems:
It uses "self.superview.bounds.size.width". This could be used if your control occupies a whole cell. However, if you have anything else in the cell which uses constraints then such code won't calculate a width correctly.
It doesn't handle rotation at all. I am pretty sure it doesn't handle other resizing events (there are bunch of less common resizing events - like a statusbar getting bigger on a phone call etc).
Do you know how to solve these problems for this case? I found a bunch of articles which talks about building more static custom controls and using pre-built controls in self-sizing cells.
However, I haven't found anything which put together a solution to handle both of these.