I have a custom font I want to use for everything displaying text in my app, labels, text views etc.
Is there a way to set the default font (labels by default use SystemFont) for the whole app?
I have a custom font I want to use for everything displaying text in my app, labels, text views etc.
Is there a way to set the default font (labels by default use SystemFont) for the whole app?
Probably not, you will probably have the set the font on your control yourself, but you can make the process easier by centralizing where you get the font types from, for example have the app delegate or some other common class have a method that returns the font, and anything needing to set the font can call that method, that will help in case you need to change your font, youd change it in one place rather than everywhere you set the fonts...Another alternative can be to make subclasses of your UI Elements that will automatically set the font, but that might be overkill..
Font type always be set in code and nib/storyboard.
For the code,just like Hugues BR said,do it in catagory can solve the problem.
For the nib/storyboard,we can Method Swizzling awakeFromNib to change font type since UI element from nib/storyboard always call it before show in the screen.
I suppose you know Aspects.It's a library for AOP programing,based on Method Swizzling. We create catagory for UILabel,UIButton,UITextView to implement it.
UILabel:
UIButton:
UITextField:
UITextView:
That's all,you can change HelveticaNeue-light to a macro with your font name.
I created my own conversion of typography for Swift 4 after reviewing a few posts, it covers most of the cases, such as:
Finally call to created method at
Appdelegate
like next:NUI is an alternative to the UIAppearance proxy. It gives you control over the font (and many other attributes) of a large number of UI element types throughout your application by simply modifying a style sheet, which can be reused across multiple applications.
After adding a
NUILabel
class to your labels, you could easily control their font in the style sheet:If you have labels with different font sizes, you could control their sizes using NUI's Label, LargeLabel, and SmallLabel classes, or even quickly create your own classes.
To complete Sandy Chapman's answer, here is a solution in Objective-C (put this category anywhere you want to change
UILabel
Appearance
):The interface file, should have this method declared publicly to be used later from places like your app delegate:
Then, you can change the
Appearance
with:Swift 4.1
Base on Fábio Oliveira's answer (https://stackoverflow.com/a/23042694/2082851), I make my own swift 4.
In short, this extension exchanges default functions
init(coder:)
,systemFont(ofSize:)
,boldSystemFont(ofSize:)
,italicSystemFont(ofSize:)
with my custom methods.Note that it's not fully implement, but you can exchange more methods base on my implementation.