Using custom font for entire iOS app swift
You can set the appearance of the UILabel and other UIViews:
UILabel.appearance().font = UIFont(name: "yourFont", size: yourSize)
More General:
AnyUIView.appearance().font = UIFont(name: "yourFont", size: yourSize)
How to use a custom font in an entire IOS app the right way
Based on this site and making some adjustments
First: You must add your fonts in your project and add them in the info.plist
file:
https://codewithchris.com/common-mistakes-with-adding-custom-fonts-to-your-ios-app/
File: UIFont.swift
import Foundation
import UIKit
struct AppFontName {
static let regular = "Montserrat-Regular"
static let bold = "Montserrat-Bold"
static let lightAlt = "Montserrat-Light"
}
//customise font
extension UIFontDescriptor.AttributeName {
static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
}
extension UIFont {
@objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.regular, size: size)!
}
@objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.bold, size: size)!
}
@objc class func myItalicSystemFont(ofSize size: CGFloat) -> UIFont {
return UIFont(name: AppFontName.lightAlt, size: size)!
}
@objc convenience init(myCoder aDecoder: NSCoder) {
guard
let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor,
let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String else {
self.init(myCoder: aDecoder)
return
}
var fontName = ""
switch fontAttribute {
case "CTFontRegularUsage":
fontName = AppFontName.regular
case "CTFontEmphasizedUsage", "CTFontBoldUsage":
fontName = AppFontName.bold
case "CTFontObliqueUsage":
fontName = AppFontName.lightAlt
default:
fontName = AppFontName.regular
}
self.init(name: fontName, size: fontDescriptor.pointSize)!
}
class func overrideInitialize() {
guard self == UIFont.self else { return }
if let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:))),
let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:))) {
method_exchangeImplementations(systemFontMethod, mySystemFontMethod)
}
if let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:))),
let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:))) {
method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)
}
if let italicSystemFontMethod = class_getClassMethod(self, #selector(italicSystemFont(ofSize:))),
let myItalicSystemFontMethod = class_getClassMethod(self, #selector(myItalicSystemFont(ofSize:))) {
method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
}
if let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:))), // Trick to get over the lack of UIFont.init(coder:))
let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:))) {
method_exchangeImplementations(initCoderMethod, myInitCoderMethod)
}
}
}
AppDelegate.swift
override init() {
super.init()
UIFont.overrideInitialize()
}
Usage:
label.font = UIFont.boldSystemFont(ofSize: 16)
label.fontUIFont.systemFont(ofSize: 12)
How to set a custom font for entire iOS app without specifying size
- (void)viewDidLoad
{
[super viewDidLoad];
[self setFontFamily:@"FagoOfficeSans-Regular" forView:self.view andSubViews:YES];
}
-(void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *lbl = (UILabel *)view;
[lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
}
if (isSubViews)
{
for (UIView *sview in view.subviews)
{
[self setFontFamily:fontFamily forView:sview andSubViews:YES];
}
}
}
Is is possible to use an imported custom font for a button in swift UI ios app?
Make sure you add the fonts correctly to your project..
check this link
Use the correct font name
button.titleLabel?.font = UIFont(name: "Lovelo Black", size: size)
Set a default font for whole iOS app?
It seems to be possible in iOS 5 using the UIAppearance proxy.
[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17.0]];
That will set the font to be whatever your custom font is for all UILabels in your app. You'll need to repeat it for each control (UIButton, UILabel, etc.).
Remember you'll need to put the UIAppFonts value in your info.plist and include the name of the font you're including.
iOS Custom font without adding to system
A storyboard does not magically look in your project for fonts. So opening a storyboard will not display a font unless that font is available on your machine as a whole.
The usual approach is to set the font in code, not in the storyboard.
Related Topics
Use a Generic Class as a Custom View in Interface Builder
Swift: How to Open a New App When Uibutton Is Tapped
How to Debug an iOS Extension (.Appex)
Type 'Any' Has No Subscript Members (Firebase)
Ios: Launch Image Multiple Language
How to Disable/Remove Firebaseanalytics
Indexpathforcell Returns Nil Since iOS7
Synchronous Url Request on Swift 2
How to Use Core Location/Gps Without Any Internet Connection/Disabled Cellular Network
iOS 9 Segue Causes App to Freeze (No Crash or Error Thrown)
How to Run Nstimer in Background Beyond 180Sec in iOS 7
How to Add a Button to the Mkpointannotation