So I have a list of users in my app stored in a UITableView
each Cell has a label that stores the users name.
I need all these labels to maintain the same size.
My main problem is auto resizing the font for each name label results in multiple different label sizes inside the tableview
So for example
"Tom Smith" is short, so font size is max
"Tommy Smithenson", longer. On smaller devices it may be resized
"Smitty Werbenjagermanjensen" is definitely too long, will be resized
I need each tableView cell to update its UILabel font size based on contents, and if a label shrinks, shrink all
I don't think hacking it to work from cellForRowAtIndexPath will work, because as you scroll a bigger name might appear resulting in some cells having different sized labels, at least until I scrolled over them again and they are dequeued..
At this point I'm not sure how to do this with Autolayout, which I would prefer.
Any ideas on how to best accomplish this?
You have two options.
1) STATIC APPROACH - Before you show your table view list, find the longest name in your contact list, calculate the font size that satisfies the layout requirements using function
func boundingRectWithSize(_ size: CGSize, options options: NSStringDrawingOptions, attributes attributes: [String : AnyObject]?, context context: NSStringDrawingContext?) -> CGRect
and set this font size to all cells.
2) DYNAMIC APPROACH - start with cells as you have it now, and once a cell get's to layoutSubviews, use notification centre to post the current size, and listen to this notification in all other cells, and if the size is smaller then their current one, apply it.