UIFontExtensions.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // CEMKit+UIFont.swift
  3. //
  4. //
  5. // Created by Cem Olcay on 12/08/15.
  6. //
  7. //
  8. #if os(iOS) || os(tvOS)
  9. import UIKit
  10. /// EZSwiftExtensions
  11. public enum FontType: String {
  12. case None = ""
  13. case Regular = "Regular"
  14. case Bold = "Bold"
  15. case DemiBold = "DemiBold"
  16. case Light = "Light"
  17. case UltraLight = "UltraLight"
  18. case Italic = "Italic"
  19. case Thin = "Thin"
  20. case Book = "Book"
  21. case Roman = "Roman"
  22. case Medium = "Medium"
  23. case MediumItalic = "MediumItalic"
  24. case CondensedMedium = "CondensedMedium"
  25. case CondensedExtraBold = "CondensedExtraBold"
  26. case SemiBold = "SemiBold"
  27. case BoldItalic = "BoldItalic"
  28. case Heavy = "Heavy"
  29. }
  30. /// EZSwiftExtensions
  31. public enum FontName: String {
  32. case HelveticaNeue
  33. case Helvetica
  34. case Futura
  35. case Menlo
  36. case Avenir
  37. case AvenirNext
  38. case Didot
  39. case AmericanTypewriter
  40. case Baskerville
  41. case Geneva
  42. case GillSans
  43. case SanFranciscoDisplay
  44. case Seravek
  45. }
  46. extension UIFont {
  47. /// EZSwiftExtensions
  48. public class func Font(_ name: FontName, type: FontType, size: CGFloat) -> UIFont! {
  49. //Using type
  50. let fontName = name.rawValue + "-" + type.rawValue
  51. if let font = UIFont(name: fontName, size: size) {
  52. return font
  53. }
  54. //That font doens't have that type, try .None
  55. let fontNameNone = name.rawValue
  56. if let font = UIFont(name: fontNameNone, size: size) {
  57. return font
  58. }
  59. //That font doens't have that type, try .Regular
  60. let fontNameRegular = name.rawValue + "-" + "Regular"
  61. if let font = UIFont(name: fontNameRegular, size: size) {
  62. return font
  63. }
  64. return nil
  65. }
  66. /// EZSwiftExtensions
  67. public class func HelveticaNeue(type: FontType, size: CGFloat) -> UIFont {
  68. return Font(.HelveticaNeue, type: type, size: size)
  69. }
  70. /// EZSwiftExtensions
  71. public class func AvenirNext(type: FontType, size: CGFloat) -> UIFont {
  72. return Font(.AvenirNext, type: type, size: size)
  73. }
  74. /// EZSwiftExtensions
  75. public class func AvenirNextDemiBold(size: CGFloat) -> UIFont {
  76. return Font(.AvenirNext, type: .DemiBold, size: size)
  77. }
  78. /// EZSwiftExtensions
  79. public class func AvenirNextRegular(size: CGFloat) -> UIFont {
  80. return Font(.AvenirNext, type: .Regular, size: size)
  81. }
  82. }
  83. #endif