UIFont+YYAdd.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // UIFont+YYAdd.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/5/11.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <UIKit/UIKit.h>
  12. #import <CoreGraphics/CoreGraphics.h>
  13. #import <CoreText/CoreText.h>
  14. NS_ASSUME_NONNULL_BEGIN
  15. /**
  16. Provides extensions for `UIFont`.
  17. */
  18. @interface UIFont (YYAdd) <NSCoding>
  19. #pragma mark - Font Traits
  20. ///=============================================================================
  21. /// @name Font Traits
  22. ///=============================================================================
  23. @property (nonatomic, readonly) BOOL isBold NS_AVAILABLE_IOS(7_0); ///< Whether the font is bold.
  24. @property (nonatomic, readonly) BOOL isItalic NS_AVAILABLE_IOS(7_0); ///< Whether the font is italic.
  25. @property (nonatomic, readonly) BOOL isMonoSpace NS_AVAILABLE_IOS(7_0); ///< Whether the font is mono space.
  26. @property (nonatomic, readonly) BOOL isColorGlyphs NS_AVAILABLE_IOS(7_0); ///< Whether the font is color glyphs (such as Emoji).
  27. @property (nonatomic, readonly) CGFloat fontWeight NS_AVAILABLE_IOS(7_0); ///< Font weight from -1.0 to 1.0. Regular weight is 0.0.
  28. /**
  29. Create a bold font from receiver.
  30. @return A bold font, or nil if failed.
  31. */
  32. - (nullable UIFont *)fontWithBold NS_AVAILABLE_IOS(7_0);
  33. /**
  34. Create a italic font from receiver.
  35. @return A italic font, or nil if failed.
  36. */
  37. - (nullable UIFont *)fontWithItalic NS_AVAILABLE_IOS(7_0);
  38. /**
  39. Create a bold and italic font from receiver.
  40. @return A bold and italic font, or nil if failed.
  41. */
  42. - (nullable UIFont *)fontWithBoldItalic NS_AVAILABLE_IOS(7_0);
  43. /**
  44. Create a normal (no bold/italic/...) font from receiver.
  45. @return A normal font, or nil if failed.
  46. */
  47. - (nullable UIFont *)fontWithNormal NS_AVAILABLE_IOS(7_0);
  48. #pragma mark - Create font
  49. ///=============================================================================
  50. /// @name Create font
  51. ///=============================================================================
  52. /**
  53. Creates and returns a font object for the specified CTFontRef.
  54. @param CTFont CoreText font.
  55. */
  56. + (nullable UIFont *)fontWithCTFont:(CTFontRef)CTFont;
  57. /**
  58. Creates and returns a font object for the specified CGFontRef and size.
  59. @param CGFont CoreGraphic font.
  60. @param size Font size.
  61. */
  62. + (nullable UIFont *)fontWithCGFont:(CGFontRef)CGFont size:(CGFloat)size;
  63. /**
  64. Creates and returns the CTFontRef object. (need call CFRelease() after used)
  65. */
  66. - (nullable CTFontRef)CTFontRef CF_RETURNS_RETAINED;
  67. /**
  68. Creates and returns the CGFontRef object. (need call CFRelease() after used)
  69. */
  70. - (nullable CGFontRef)CGFontRef CF_RETURNS_RETAINED;
  71. #pragma mark - Load and unload font
  72. ///=============================================================================
  73. /// @name Load and unload font
  74. ///=============================================================================
  75. /**
  76. Load the font from file path. Support format:TTF,OTF.
  77. If return YES, font can be load use it PostScript Name: [UIFont fontWithName:...]
  78. @param path font file's full path
  79. */
  80. + (BOOL)loadFontFromPath:(NSString *)path;
  81. /**
  82. Unload font from file path.
  83. @param path font file's full path
  84. */
  85. + (void)unloadFontFromPath:(NSString *)path;
  86. /**
  87. Load the font from data. Support format:TTF,OTF.
  88. @param data Font data.
  89. @return UIFont object if load succeed, otherwise nil.
  90. */
  91. + (nullable UIFont *)loadFontFromData:(NSData *)data;
  92. /**
  93. Unload font which is loaded by loadFontFromData: function.
  94. @param font the font loaded by loadFontFromData: function
  95. @return YES if succeed, otherwise NO.
  96. */
  97. + (BOOL)unloadFontFromData:(UIFont *)font;
  98. #pragma mark - Dump font data
  99. ///=============================================================================
  100. /// @name Dump font data
  101. ///=============================================================================
  102. /**
  103. Serialize and return the font data.
  104. @param font The font.
  105. @return data in TTF, or nil if an error occurs.
  106. */
  107. + (nullable NSData *)dataFromFont:(UIFont *)font;
  108. /**
  109. Serialize and return the font data.
  110. @param cgFont The font.
  111. @return data in TTF, or nil if an error occurs.
  112. */
  113. + (nullable NSData *)dataFromCGFont:(CGFontRef)cgFont;
  114. @end
  115. NS_ASSUME_NONNULL_END