123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- //
- // NSString+YYAdd.h
- // YYCategories <https://github.com/ibireme/YYCategories>
- //
- // Created by ibireme on 13/4/3.
- // Copyright (c) 2015 ibireme.
- //
- // This source code is licensed under the MIT-style license found in the
- // LICENSE file in the root directory of this source tree.
- //
- #import <UIKit/UIKit.h>
- /**
- Provide hash, encrypt, encode and some common method for 'NSString'.
- */
- @interface NSString (YYAdd)
- #pragma mark - Drawing
- ///=============================================================================
- /// @name Drawing
- ///=============================================================================
- /**
- Returns the size of the string if it were rendered with the specified constraints.
-
- @param font The font to use for computing the string size.
-
- @param size The maximum acceptable size for the string. This value is
- used to calculate where line breaks and wrapping would occur.
-
- @param lineBreakMode The line break options for computing the size of the string.
- For a list of possible values, see NSLineBreakMode.
-
- @return The width and height of the resulting string's bounding box.
- These values may be rounded up to the nearest whole number.
- */
- - (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode;
- /**
- Returns the width of the string if it were to be rendered with the specified
- font on a single line.
-
- @param font The font to use for computing the string width.
-
- @return The width of the resulting string's bounding box. These values may be
- rounded up to the nearest whole number.
- */
- - (CGFloat)widthForFont:(UIFont *)font;
- /**
- Returns the height of the string if it were rendered with the specified constraints.
-
- @param font The font to use for computing the string size.
-
- @param width The maximum acceptable width for the string. This value is used
- to calculate where line breaks and wrapping would occur.
-
- @return The height of the resulting string's bounding box. These values
- may be rounded up to the nearest whole number.
- */
- - (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width;
- #pragma mark - Regular Expression
- ///=============================================================================
- /// @name Regular Expression
- ///=============================================================================
- /**
- Whether it can match the regular expression
-
- @param regex The regular expression
- @param options The matching options to report.
- @return YES if can match the regex; otherwise, NO.
- */
- - (BOOL)matchesRegex:(NSString *)regex options:(NSRegularExpressionOptions)options;
- /**
- Match the regular expression, and executes a given block using each object in the matches.
-
- @param regex The regular expression
- @param options The matching options to report.
- @param block The block to apply to elements in the array of matches.
- The block takes four arguments:
- match: The match substring.
- matchRange: The matching options.
- stop: A reference to a Boolean value. The block can set the value
- to YES to stop further processing of the array. The stop
- argument is an out-only argument. You should only ever set
- this Boolean to YES within the Block.
- */
- - (void)enumerateRegexMatches:(NSString *)regex
- options:(NSRegularExpressionOptions)options
- usingBlock:(void (^)(NSString *match, NSRange matchRange, BOOL *stop))block;
- /**
- Returns a new string containing matching regular expressions replaced with the template string.
-
- @param regex The regular expression
- @param options The matching options to report.
- @param replacement The substitution template used when replacing matching instances.
-
- @return A string with matching regular expressions replaced by the template string.
- */
- - (NSString *)stringByReplacingRegex:(NSString *)regex
- options:(NSRegularExpressionOptions)options
- withString:(NSString *)replacement;
- #pragma mark - Emoji
- ///=============================================================================
- /// @name Emoji
- ///=============================================================================
- /**
- Whether the receiver contains Apple Emoji (displayed in current version of iOS).
- */
- - (BOOL)containsEmoji;
- - (BOOL)containsEmojiForSystemVersion:(float)systemVersion;
- #pragma mark - Utilities
- ///=============================================================================
- /// @name Utilities
- ///=============================================================================
- /**
- Returns a new UUID NSString
- e.g. "D1178E50-2A4D-4F1F-9BD3-F6AAB00E06B1"
- */
- + (NSString *)stringWithUUID;
- /**
- Returns a string containing the characters in a given UTF32Char.
-
- @param char32 A UTF-32 character.
- @return A new string, or nil if the character is invalid.
- */
- + (NSString *)stringWithUTF32Char:(UTF32Char)char32;
- /**
- Returns a string containing the characters in a given UTF32Char array.
-
- @param char32 An array of UTF-32 character.
- @param length The character count in array.
- @return A new string, or nil if an error occurs.
- */
- + (NSString *)stringWithUTF32Chars:(const UTF32Char *)char32 length:(NSUInteger)length;
- /**
- Enumerates the unicode characters (UTF-32) in the specified range of the string.
-
- @param range The range within the string to enumerate substrings.
- @param block The block executed for the enumeration. The block takes four arguments:
- char32: The unicode character.
- range: The range in receiver. If the range.length is 1, the character is in BMP;
- otherwise (range.length is 2) the character is in none-BMP Plane and stored
- by a surrogate pair in the receiver.
- stop: A reference to a Boolean value that the block can use to stop the enumeration
- by setting *stop = YES; it should not touch *stop otherwise.
- */
- - (void)enumerateUTF32CharInRange:(NSRange)range usingBlock:(void (^)(UTF32Char char32, NSRange range, BOOL *stop))block;
- /**
- Trim blank characters (space and newline) in head and tail.
- @return the trimmed string.
- */
- - (NSString *)stringByTrim;
- /**
- Add scale modifier to the file name (without path extension),
- From @"name" to @"name@2x".
-
- e.g.
- <table>
- <tr><th>Before </th><th>After(scale:2)</th></tr>
- <tr><td>"icon" </td><td>"icon@2x" </td></tr>
- <tr><td>"icon " </td><td>"icon @2x" </td></tr>
- <tr><td>"icon.top" </td><td>"icon.top@2x" </td></tr>
- <tr><td>"/p/name" </td><td>"/p/name@2x" </td></tr>
- <tr><td>"/path/" </td><td>"/path/" </td></tr>
- </table>
-
- @param scale Resource scale.
- @return String by add scale modifier, or just return if it's not end with file name.
- */
- - (NSString *)stringByAppendingNameScale:(CGFloat)scale;
- /**
- Add scale modifier to the file path (with path extension),
- From @"name.png" to @"name@2x.png".
-
- e.g.
- <table>
- <tr><th>Before </th><th>After(scale:2)</th></tr>
- <tr><td>"icon.png" </td><td>"icon@2x.png" </td></tr>
- <tr><td>"icon..png"</td><td>"icon.@2x.png"</td></tr>
- <tr><td>"icon" </td><td>"icon@2x" </td></tr>
- <tr><td>"icon " </td><td>"icon @2x" </td></tr>
- <tr><td>"icon." </td><td>"icon.@2x" </td></tr>
- <tr><td>"/p/name" </td><td>"/p/name@2x" </td></tr>
- <tr><td>"/path/" </td><td>"/path/" </td></tr>
- </table>
-
- @param scale Resource scale.
- @return String by add scale modifier, or just return if it's not end with file name.
- */
- - (NSString *)stringByAppendingPathScale:(CGFloat)scale;
- /**
- Return the path scale.
-
- e.g.
- <table>
- <tr><th>Path </th><th>Scale </th></tr>
- <tr><td>"icon.png" </td><td>1 </td></tr>
- <tr><td>"icon@2x.png" </td><td>2 </td></tr>
- <tr><td>"icon@2.5x.png" </td><td>2.5 </td></tr>
- <tr><td>"icon@2x" </td><td>1 </td></tr>
- <tr><td>"icon@2x..png" </td><td>1 </td></tr>
- <tr><td>"icon@2x.png/" </td><td>1 </td></tr>
- </table>
- */
- - (CGFloat)pathScale;
- /**
- nil, @"", @" ", @"\n" will Returns NO; otherwise Returns YES.
- */
- - (BOOL)isNotBlank;
- /**
- Returns YES if the target string is contained within the receiver.
- @param string A string to test the the receiver.
-
- @discussion Apple has implemented this method in iOS8.
- */
- - (BOOL)containsString:(NSString *)string;
- /**
- Returns YES if the target CharacterSet is contained within the receiver.
- @param set A character set to test the the receiver.
- */
- - (BOOL)containsCharacterSet:(NSCharacterSet *)set;
- /**
- Returns NSMakeRange(0, self.length).
- */
- - (NSRange)rangeOfAll;
- /**
- Create a string from the file in main bundle (similar to [UIImage imageNamed:]).
-
- @param name The file name (in main bundle).
-
- @return A new string create from the file in UTF-8 character encoding.
- */
- + (NSString *)stringNamed:(NSString *)name;
- @end
|