Rightpoint / BonMot

Beautiful, easy attributed strings in Swift
MIT License
3.54k stars 196 forks source link

Strange Behavior with Dynamic Type Updates #402

Open nevillco opened 3 years ago

nevillco commented 3 years ago

Hey Zev/Rightpointers! Hope you're doing well. I'm seeing some weird behavior with Dynamic Type and application.enableAdaptiveContentSizeMonitor() I'm wondering if anyone has context on.

I have two StringStyles: one just uses the system body font (UIFont.preferredFont(for: .body)). The other uses a font in the bundle (Lato), and I create a scaled font like this:

let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .body)
let size = descriptor.pointSize
// swiftlint:disable:next force_unwrapping
return UIFont(name: customFont.name, size: size)!

In the demo video attached, I have 4 labels: applying both .adapt(body) and .adapt(control) to each of these styles. I'm also calling application.enableAdaptiveContentSizeMonitor(), so I'm hoping to see the labels responding to changes to Dynamic Type when I return to the app. Weirdly, what I'm seeing is that all 4 labels do adjust their size somewhat in the right direction (larger or smaller), but I still need to leave and recreate the view to get the correct dynamic size. In the video attached, the labels are initially the correct (small) size, and when I move the Dynamic Type slider to the large end, the labels get bigger, but not as big as expected. Once I leave the view and push to a new view, they're the correct size again. I see the same behavior regardless of whichever 2 points on the Dynamic Type slider I use. Any idea what might be causing this weird behavior?

https://user-images.githubusercontent.com/7501762/104847690-ab379600-58af-11eb-8ca0-90af7c56cee0.mov

ZevEisenberg commented 3 years ago

Weird. Nothing immediately jumps out. Keep in mind that BonMot's dynamic type stuff was created before the more recent UIKit APIs for dynamic type metrics, so in addition to debugging this, it's possible that there's a more modern rethink that we could do.

In terms of debugging, you can't go wrong subclassing UILabel and observing its font and attributedText properties to see if anything interesting pops up.