Thursday, October 13, 2016
How to figure out the font to assign it programatically in swift
Assigning fonts to texts programatically can be a pain due to the parameters passed being a hardcoded string, hence no auto completion, what's worse is the font's name isn't always the same as the filename. Furthermore fonts are grouped by font families, which may contain multiple fonts.
Firstly, run print(UIFont.familyNames) to print out every single family name in an array.
["Copperplate", "Heiti SC", "Times New Roman"]
You can then
print(UIFont.fontNames(forFamilyName: "Times New Roman"))
Which outputs to
["TimesNewRomanPSMT", "TimesNewRomanPS-BoldItalicMT", "TimesNewRomanPS-ItalicMT", "TimesNewRomanPS-BoldMT"]
Then you can finally let font = UIFont(name: "TimesNewRomanPS-BoldMT", size: 16)
Subscribe to:
Posts (Atom)