Guest User

Untitled

a guest
Dec 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. import UIKit
  2.  
  3. struct AppFontName {
  4. static let regular = "CourierNewPSMT"
  5. static let bold = "CourierNewPS-BoldMT"
  6. static let italic = "CourierNewPS-ItalicMT"
  7. }
  8.  
  9. extension UIFontDescriptor.AttributeName {
  10. static let nsctFontUIUsage = UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")
  11. }
  12.  
  13. extension UIFont {
  14.  
  15. @objc class func mySystemFont(ofSize size: CGFloat) -> UIFont {
  16. return UIFont(name: AppFontName.regular, size: size)!
  17. }
  18.  
  19. @objc class func myBoldSystemFont(ofSize size: CGFloat) -> UIFont {
  20. return UIFont(name: AppFontName.bold, size: size)!
  21. }
  22.  
  23. @objc class func myItalicSystemFont(ofSize size: CGFloat) -> UIFont {
  24. return UIFont(name: AppFontName.italic, size: size)!
  25. }
  26.  
  27. @objc convenience init(myCoder aDecoder: NSCoder) {
  28. guard
  29. let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor,
  30. let fontAttribute = fontDescriptor.fontAttributes[.nsctFontUIUsage] as? String else {
  31. self.init(myCoder: aDecoder)
  32. return
  33. }
  34. var fontName = ""
  35. switch fontAttribute {
  36. case "CTFontRegularUsage":
  37. fontName = AppFontName.regular
  38. case "CTFontEmphasizedUsage", "CTFontBoldUsage":
  39. fontName = AppFontName.bold
  40. case "CTFontObliqueUsage":
  41. fontName = AppFontName.italic
  42. default:
  43. fontName = AppFontName.regular
  44. }
  45. self.init(name: fontName, size: fontDescriptor.pointSize)!
  46. }
  47.  
  48. class func overrideInitialize() {
  49. guard self == UIFont.self else { return }
  50.  
  51. if let systemFontMethod = class_getClassMethod(self, #selector(systemFont(ofSize:))),
  52. let mySystemFontMethod = class_getClassMethod(self, #selector(mySystemFont(ofSize:))) {
  53. method_exchangeImplementations(systemFontMethod, mySystemFontMethod)
  54. }
  55.  
  56. if let boldSystemFontMethod = class_getClassMethod(self, #selector(boldSystemFont(ofSize:))),
  57. let myBoldSystemFontMethod = class_getClassMethod(self, #selector(myBoldSystemFont(ofSize:))) {
  58. method_exchangeImplementations(boldSystemFontMethod, myBoldSystemFontMethod)
  59. }
  60.  
  61. if let italicSystemFontMethod = class_getClassMethod(self, #selector(italicSystemFont(ofSize:))),
  62. let myItalicSystemFontMethod = class_getClassMethod(self, #selector(myItalicSystemFont(ofSize:))) {
  63. method_exchangeImplementations(italicSystemFontMethod, myItalicSystemFontMethod)
  64. }
  65.  
  66. if let initCoderMethod = class_getInstanceMethod(self, #selector(UIFontDescriptor.init(coder:))), // Trick to get over the lack of UIFont.init(coder:))
  67. let myInitCoderMethod = class_getInstanceMethod(self, #selector(UIFont.init(myCoder:))) {
  68. method_exchangeImplementations(initCoderMethod, myInitCoderMethod)
  69. }
  70. }
  71. }
  72.  
  73.  
  74. class AppDelegate: UIResponder, UIApplicationDelegate {
  75. // Avoid warning of Swift
  76. // Method 'initialize()' defines Objective-C class method 'initialize', which is not guaranteed to be invoked by Swift and will be disallowed in future versions
  77. override init() {
  78. super.init()
  79. UIFont.overrideInitialize()
  80. }
  81. ...
  82. }
Add Comment
Please, Sign In to add comment