Dynamic Text Resizing in Swift

Creating a great user experience lies in making every interaction feel nature and intended to the user.

In a recent project, to make interactions feel more catered, I needed a UILabel to have it’s text scale to fit its bounds to the best extent that it can.

Usage

The implementation can be extended for any UI element (such as UILabel):

let label = UILabel()
label.font = UIFont.preferredFont(forTextStyle: .headline)

iOS Font Sizes

StyleFontSize
.largeTitleSFUIDisplay34.0
.title1SFUIDisplay 
(-Light on iOS <=10)
28.0
.title2SFUIDisplay22.0
.title3SFUIDisplay20.0
.headlineSFUIText-Semibold17.0
.calloutSFUIText16.0
.subheadlineSFUIText15.0
.bodySFUIText17.0
.footnoteSFUIText13.0
.caption1SFUIText12.0
.caption2SFUIText11.0

Leave a Reply

Your email address will not be published. Required fields are marked *