Setting up custom font style for Text, Labels and Buttons globally in iOS Application — iOS Swift
UI Element Extensions, @IBInspectable, Custom fonts and System custom fonts.
While developing large scale iOS App in which we have multiple number of screens and nibs we always have to set Font Family, Style and Size individually to each Label, Text Fields, Button Titles and etc. It cost a lot of repetitive effort and it gives you boring vibes. (* ̄O ̄)ノ
We are here to make it quick and more interesting.
We are going to use @IBInspectable and minimise the repetitive effort for you. @IBInspectable allows us to create attributes in code that we can assign in a storyboard or a .xib
file.
Let’s jump into the code and implementation which is very simple to understand.
I have created singleton class of FontDictionary where we have allFont as Dictionary type for saving Family-Style and Size for all the fonts.
name — “Font Family”-“Font Style”
size — “Font Size”
Example — We have a custom font of type body1 type in which we have Font Family as Montserrat, Font Styler as Bold and Font Size as 16.0.
allFont[body1][name] — “Montserrat-Bold”
allFont[body1][size] — “16.0”
And now the wait is over now, let me introduce you to the extension of UILabel is which we are adding @IBInspectable and its responsible for setting the Customs, size and family. Isn’t that cool!
Same you can add extension for UITextView, UITextField and UIButton.
And now a new attribute with name of Style you will see in the attribute inspector in Storyboard. When you enter “body1” or “body2” in this field it means our extension will understand it as “Montserrat-Bold” with “16.0” size.
This is how my ViewController looks like. I have taken textLabels, textView, button and textField for example.
Let’s take a look at output —
“body2” — textView, textLabel2, textField
“body1” — textLabel1, Button
Add On :
You can also add plist File to save all of our font style. Then we don’t have to use our FontDictionary class to load the data of our fonts and also plist file will load as our app launches.
plist File(Information property list file): — It is a structured text file that contains essential configuration information for a bundled executable. The file itself is typically encoded using the Unicode UTF-8 encoding and the contents are structured using XML. The root XML node is a dictionary, whose contents are a set of keys and values describing different aspects of the bundle.
When to use this — consider the scenario where your are fetching a number of configurations at launch.
I have created an FontTypes custom plist file.
You can load the data from plist like this and use it. And now you can use allFontPlist in place of FontDictionary.fontDictionary.allFont in the extensions and no need of FontDictionary class now.
Conclusion:
There is no standard way to setting a custom font for an entire iOS Application. You can use these extensions on UILabel, UITextField, UITextView and UIButton. This simplest way gives us the control to add font globally. Don’t forget to give claps 👏 “My first tech article”.…