Set a Default Font For Whole iOS App

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.

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)

Is there a way to change default font for your application

The answer is no, you cant change apple's systemFont, you need to set the font on your control yourself.

For best way to set a default font for whole iOS app, please check the below SO questions:

Set a default font for whole iOS app?

How to set a custom font for entire iOS app without specifying size

How do I set a custom font for the whole application?

Is there a simple way to set a default font for the whole app?

Set font universally for whole app

Try this :)

Obj-C

[[UILabel appearance] setFont:[UIFont fontWithName:@"YourFontName" size:17.0]];

Swift

UILabel.appearance().font = UIFont(name: "YourFontName", size: 17.0)

Be sure to add your chosen font to your projects target if you're using a custom font.

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)


Related Topics



Leave a reply



Submit