MikeJ
MikeJ

Reputation: 2437

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?

Upvotes: 5

Views: 3127

Answers (2)

Kiet Nguyen
Kiet Nguyen

Reputation: 619

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

Upvotes: 1

Peter Hornsby
Peter Hornsby

Reputation: 4266

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

Upvotes: 1

Related Questions