Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set font size using size classes programmatically

There are a few posts on this, but none seem to have a complete solution.

Using adaptive layout via IB I can easily change font sizes for specific size classes, however I have a custom view using UILabels and I want to programmatically set the font size depending on the size class in use (using swift) - Has anyone else had this issue or come across a solution?

like image 823
MikeJ Avatar asked Oct 30 '22 11:10

MikeJ


2 Answers

You need your custom view to override traitCollectionDidChange(previousTraitCollection: UITraitCollection?) from the UITraitEnvironment Protocol that UIView conforms to.

Discussion

The system calls this method when the iOS interface environment changes. Implement this method in view controllers and views, according to your app’s needs, to respond to such changes. For example, you might adjust the layout of the subviews of a view controller when an iPhone is rotated from portrait to landscape orientation. The default implementation of this method is empty.

You may have to observe height/width for the iPad size class: Regular| Regular

like image 84
Peter Hornsby Avatar answered Nov 15 '22 05:11

Peter Hornsby


Your custom view (UIView) has a traitCollection property that has the size classes information. Check out the answer here https://stackoverflow.com/a/25795175/2503640

like image 30
Kiet Nguyen Avatar answered Nov 15 '22 07:11

Kiet Nguyen