A short thread on text, fonts and performance. Things I noticed when working with various of apps and frameworks across Apple platforms, mostly iOS. (1/?)
String formatting is extremely expensive. Many people like to use stringWithFormat: ( https://developer.apple.com/documentation/foundation/nsstring/1497275-stringwithformat?language=objc) to build cache keys, please don’t, the cost is usually more than you think. Use a real class for cache key, and provide your own hash function would be much better.
On iOS, UILabel is generally much more efficient than UITextField and UITextView. If you really need text editing, it is usually more efficient to present all as UILabels and swap the ones you want to edit to text field/view on demand.
NS/CF Mutable dictionary/attributed strings are much more expensive than immutable ones. Try to use immutable ones as much as possible, although that might make writing certain code slightly inconvenient.
Swift bridging dictionary/array/string to NSDictionary/NSArray/NSString can have a lot of hidden costs, they have improved a lot but do measure with Instruments and for frequently called code try to avoid such frequent bridging conversion as much as possible.
Using SF Symbols is great but performance can suffer when you embed them NSAttributedString as NSTextAttachment. If you can, keep using UI/NSImageView will perform better.
Hyphenation is expensive! In iOS 14 it received some great improvements but do pick your hyphenation factor ( https://developer.apple.com/documentation/uikit/nsparagraphstyle/1529275-hyphenationfactor) carefully to avoid using hyphenation too much in frequently scrolled text.
Text layout is often the most expensive part of UI layout. Avoid setting the attributes of a control too often, for example if you are setting UILabel attributedText then first put all attributes together then set it in one call. Avoid putting custom attributes in the attrString.
Always measure when making changes related to performance, try to pick the slowest device available. iPhone 6s Plus for example would be a good choice and still supports iOS 14.
You can follow @jjgod.
Tip: mention @twtextapp on a Twitter thread with the keyword “unroll” to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.