NSString+YYAdd.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // NSString+YYAdd.h
  3. // YYCategories <https://github.com/ibireme/YYCategories>
  4. //
  5. // Created by ibireme on 13/4/3.
  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. /**
  13. Provide hash, encrypt, encode and some common method for 'NSString'.
  14. */
  15. @interface NSString (YYAdd)
  16. #pragma mark - Drawing
  17. ///=============================================================================
  18. /// @name Drawing
  19. ///=============================================================================
  20. /**
  21. Returns the size of the string if it were rendered with the specified constraints.
  22. @param font The font to use for computing the string size.
  23. @param size The maximum acceptable size for the string. This value is
  24. used to calculate where line breaks and wrapping would occur.
  25. @param lineBreakMode The line break options for computing the size of the string.
  26. For a list of possible values, see NSLineBreakMode.
  27. @return The width and height of the resulting string's bounding box.
  28. These values may be rounded up to the nearest whole number.
  29. */
  30. - (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode;
  31. /**
  32. Returns the width of the string if it were to be rendered with the specified
  33. font on a single line.
  34. @param font The font to use for computing the string width.
  35. @return The width of the resulting string's bounding box. These values may be
  36. rounded up to the nearest whole number.
  37. */
  38. - (CGFloat)widthForFont:(UIFont *)font;
  39. /**
  40. Returns the height of the string if it were rendered with the specified constraints.
  41. @param font The font to use for computing the string size.
  42. @param width The maximum acceptable width for the string. This value is used
  43. to calculate where line breaks and wrapping would occur.
  44. @return The height of the resulting string's bounding box. These values
  45. may be rounded up to the nearest whole number.
  46. */
  47. - (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width;
  48. #pragma mark - Regular Expression
  49. ///=============================================================================
  50. /// @name Regular Expression
  51. ///=============================================================================
  52. /**
  53. Whether it can match the regular expression
  54. @param regex The regular expression
  55. @param options The matching options to report.
  56. @return YES if can match the regex; otherwise, NO.
  57. */
  58. - (BOOL)matchesRegex:(NSString *)regex options:(NSRegularExpressionOptions)options;
  59. /**
  60. Match the regular expression, and executes a given block using each object in the matches.
  61. @param regex The regular expression
  62. @param options The matching options to report.
  63. @param block The block to apply to elements in the array of matches.
  64. The block takes four arguments:
  65. match: The match substring.
  66. matchRange: The matching options.
  67. stop: A reference to a Boolean value. The block can set the value
  68. to YES to stop further processing of the array. The stop
  69. argument is an out-only argument. You should only ever set
  70. this Boolean to YES within the Block.
  71. */
  72. - (void)enumerateRegexMatches:(NSString *)regex
  73. options:(NSRegularExpressionOptions)options
  74. usingBlock:(void (^)(NSString *match, NSRange matchRange, BOOL *stop))block;
  75. /**
  76. Returns a new string containing matching regular expressions replaced with the template string.
  77. @param regex The regular expression
  78. @param options The matching options to report.
  79. @param replacement The substitution template used when replacing matching instances.
  80. @return A string with matching regular expressions replaced by the template string.
  81. */
  82. - (NSString *)stringByReplacingRegex:(NSString *)regex
  83. options:(NSRegularExpressionOptions)options
  84. withString:(NSString *)replacement;
  85. #pragma mark - Emoji
  86. ///=============================================================================
  87. /// @name Emoji
  88. ///=============================================================================
  89. /**
  90. Whether the receiver contains Apple Emoji (displayed in current version of iOS).
  91. */
  92. - (BOOL)containsEmoji;
  93. - (BOOL)containsEmojiForSystemVersion:(double)systemVersion;
  94. #pragma mark - Utilities
  95. ///=============================================================================
  96. /// @name Utilities
  97. ///=============================================================================
  98. /**
  99. Returns a new UUID NSString
  100. e.g. "D1178E50-2A4D-4F1F-9BD3-F6AAB00E06B1"
  101. */
  102. + (NSString *)stringWithUUID;
  103. /**
  104. Returns a string containing the characters in a given UTF32Char.
  105. @param char32 A UTF-32 character.
  106. @return A new string, or nil if the character is invalid.
  107. */
  108. + (NSString *)stringWithUTF32Char:(UTF32Char)char32;
  109. /**
  110. Returns a string containing the characters in a given UTF32Char array.
  111. @param char32 An array of UTF-32 character.
  112. @param length The character count in array.
  113. @return A new string, or nil if an error occurs.
  114. */
  115. + (NSString *)stringWithUTF32Chars:(const UTF32Char *)char32 length:(NSUInteger)length;
  116. /**
  117. Enumerates the unicode characters (UTF-32) in the specified range of the string.
  118. @param range The range within the string to enumerate substrings.
  119. @param block The block executed for the enumeration. The block takes four arguments:
  120. char32: The unicode character.
  121. range: The range in receiver. If the range.length is 1, the character is in BMP;
  122. otherwise (range.length is 2) the character is in none-BMP Plane and stored
  123. by a surrogate pair in the receiver.
  124. stop: A reference to a Boolean value that the block can use to stop the enumeration
  125. by setting *stop = YES; it should not touch *stop otherwise.
  126. */
  127. - (void)enumerateUTF32CharInRange:(NSRange)range usingBlock:(void (^)(UTF32Char char32, NSRange range, BOOL *stop))block;
  128. /**
  129. Trim blank characters (space and newline) in head and tail.
  130. @return the trimmed string.
  131. */
  132. - (NSString *)stringByTrim;
  133. /**
  134. Add scale modifier to the file name (without path extension),
  135. From @"name" to @"name@2x".
  136. e.g.
  137. <table>
  138. <tr><th>Before </th><th>After(scale:2)</th></tr>
  139. <tr><td>"icon" </td><td>"icon@2x" </td></tr>
  140. <tr><td>"icon " </td><td>"icon @2x" </td></tr>
  141. <tr><td>"icon.top" </td><td>"icon.top@2x" </td></tr>
  142. <tr><td>"/p/name" </td><td>"/p/name@2x" </td></tr>
  143. <tr><td>"/path/" </td><td>"/path/" </td></tr>
  144. </table>
  145. @param scale Resource scale.
  146. @return String by add scale modifier, or just return if it's not end with file name.
  147. */
  148. - (NSString *)stringByAppendingNameScale:(CGFloat)scale;
  149. /**
  150. Add scale modifier to the file path (with path extension),
  151. From @"name.png" to @"name@2x.png".
  152. e.g.
  153. <table>
  154. <tr><th>Before </th><th>After(scale:2)</th></tr>
  155. <tr><td>"icon.png" </td><td>"icon@2x.png" </td></tr>
  156. <tr><td>"icon..png"</td><td>"icon.@2x.png"</td></tr>
  157. <tr><td>"icon" </td><td>"icon@2x" </td></tr>
  158. <tr><td>"icon " </td><td>"icon @2x" </td></tr>
  159. <tr><td>"icon." </td><td>"icon.@2x" </td></tr>
  160. <tr><td>"/p/name" </td><td>"/p/name@2x" </td></tr>
  161. <tr><td>"/path/" </td><td>"/path/" </td></tr>
  162. </table>
  163. @param scale Resource scale.
  164. @return String by add scale modifier, or just return if it's not end with file name.
  165. */
  166. - (NSString *)stringByAppendingPathScale:(CGFloat)scale;
  167. /**
  168. Return the path scale.
  169. e.g.
  170. <table>
  171. <tr><th>Path </th><th>Scale </th></tr>
  172. <tr><td>"icon.png" </td><td>1 </td></tr>
  173. <tr><td>"icon@2x.png" </td><td>2 </td></tr>
  174. <tr><td>"icon@2.5x.png" </td><td>2.5 </td></tr>
  175. <tr><td>"icon@2x" </td><td>1 </td></tr>
  176. <tr><td>"icon@2x..png" </td><td>1 </td></tr>
  177. <tr><td>"icon@2x.png/" </td><td>1 </td></tr>
  178. </table>
  179. */
  180. - (CGFloat)pathScale;
  181. /**
  182. nil, @"", @" ", @"\n" will Returns NO; otherwise Returns YES.
  183. */
  184. - (BOOL)isNotBlank;
  185. /**
  186. Returns YES if the target string is contained within the receiver.
  187. @param string A string to test the the receiver.
  188. @discussion Apple has implemented this method in iOS8.
  189. */
  190. - (BOOL)containsString:(NSString *)string;
  191. /**
  192. Returns YES if the target CharacterSet is contained within the receiver.
  193. @param set A character set to test the the receiver.
  194. */
  195. - (BOOL)containsCharacterSet:(NSCharacterSet *)set;
  196. /**
  197. Returns NSMakeRange(0, self.length).
  198. */
  199. - (NSRange)rangeOfAll;
  200. /**
  201. Create a string from the file in main bundle (similar to [UIImage imageNamed:]).
  202. @param name The file name (in main bundle).
  203. @return A new string create from the file in UTF-8 character encoding.
  204. */
  205. + (NSString *)stringNamed:(NSString *)name;
  206. @end