UIFont+Extension.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // UIFont+Extension.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by mac on 2024/4/10.
  6. //
  7. import UIKit
  8. // MARK: - 常用的系统基本字体扩展
  9. extension UIFont {
  10. // MARK: 1.1、默认字体
  11. /// 默认字体
  12. /// - Parameter ofSize: 字体大小
  13. /// - Returns: 字体
  14. static func textF(_ ofSize: CGFloat) -> UIFont {
  15. return text(ofSize, W: .regular)
  16. }
  17. // MARK: 1.2、常规字体
  18. /// 常规字体
  19. /// - Parameter ofSize: 字体大小
  20. /// - Returns: 字体
  21. static func textR(_ ofSize: CGFloat) -> UIFont {
  22. return text(ofSize, W: .regular)
  23. }
  24. // MARK: 1.3、中等的字体
  25. /// - Parameter ofSize: 字体大小
  26. /// - Returns: 字体
  27. static func textM(_ ofSize: CGFloat) -> UIFont {
  28. return text(ofSize, W: .medium)
  29. }
  30. // MARK: 1.4、加粗的字体
  31. /// 加粗的字体
  32. /// - Parameter ofSize: 字体大小
  33. /// - Returns: 字体
  34. static func textB(_ ofSize: CGFloat) -> UIFont {
  35. return text(ofSize, W: .bold)
  36. }
  37. // MARK: 1.5、半粗体的字体
  38. /// 半粗体的字体
  39. /// - Parameter ofSize: 字体大小
  40. /// - Returns: 字体
  41. static func textSB(_ ofSize: CGFloat) -> UIFont {
  42. return text(ofSize, W: .semibold)
  43. }
  44. // MARK: 1.6、超细的字体
  45. /// 超细的字体
  46. /// - Parameter ofSize: 字体大小
  47. /// - Returns: 字体
  48. static func textUltraLight(_ ofSize: CGFloat) -> UIFont {
  49. return text(ofSize, W: .ultraLight)
  50. }
  51. // MARK: 1.7、纤细的字体
  52. /// 纤细的字体
  53. /// - Parameter ofSize: 字体大小
  54. /// - Returns: 字体
  55. static func textThin(_ ofSize: CGFloat) -> UIFont {
  56. return text(ofSize, W: .thin)
  57. }
  58. // MARK: 1.8、亮字体
  59. /// 亮字体
  60. /// - Parameter ofSize: 字体大小
  61. /// - Returns: 字体
  62. static func textLight(_ ofSize: CGFloat) -> UIFont {
  63. return text(ofSize, W: .light)
  64. }
  65. // MARK: 1.9、介于Bold和Black之间
  66. /// 介于Bold和Black之间
  67. /// - Parameter ofSize: 字体大小
  68. /// - Returns: 字体
  69. static func textHeavy(_ ofSize: CGFloat) -> UIFont {
  70. return text(ofSize, W: .heavy)
  71. }
  72. // MARK: 1.10、最粗字体
  73. /// 最粗字体
  74. /// - Parameter ofSize: 字体大小
  75. /// - Returns: 字体
  76. static func textBlack(_ ofSize: CGFloat) -> UIFont {
  77. return text(ofSize, W: .black)
  78. }
  79. /// 文字字体
  80. fileprivate static func text(_ ofSize: CGFloat, W Weight: UIFont.Weight) -> UIFont {
  81. let scaleSize = ofSize.rpx
  82. return UIFont.systemFont(ofSize: scaleSize, weight: Weight)
  83. }
  84. func withTraits(traits: UIFontDescriptor.SymbolicTraits) -> UIFont {
  85. let descriptor = fontDescriptor.withSymbolicTraits(traits)
  86. return UIFont(descriptor: descriptor!, size: 0)
  87. }
  88. }