Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- struct AppFontName {
- static let regular = "CourierNewPSMT"
- static let bold = "CourierNewPS-BoldMT"
- static let italic = "CourierNewPS-ItalicMT"
- }
- 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.italic, 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.italic
- 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)
- }
- }
- }
- class AppDelegate: UIResponder, UIApplicationDelegate {
- // Avoid warning of Swift
- // Method 'initialize()' defines Objective-C class method 'initialize', which is not guaranteed to be invoked by Swift and will be disallowed in future versions
- override init() {
- super.init()
- UIFont.overrideInitialize()
- }
- ...
- }
Add Comment
Please, Sign In to add comment