Adventures with NSString.sizeWithAttributes
I’ve been working on an update to my app, EzMileage. In one screen I’m showing lots of text in table cells, and I’m adjusting the label sizes based on the actual content.
Now this app hasn’t been updated in many years, and the current version on the App Store was released when iOS 6 was the newest and shiniest version.
To calculate the required size for the text I was using NSString’s sizeWithFont: method. This method was deprecated in iOS 7 and is not even available to me since I’m using Swift (if I was using Objective-C I could still use it and ignore the deprecation warning).
This shouldn’t be a problem because there is a replacement: sizeWithAttributes:. The straightforward way to use it was, I thought, to use the attributes from the label’s font’s UIFontDescriptor:
This returns a different size than the old sizeWithFont:!
The solution turned out to be to create my own font attributes from scratch: