Created
May 24, 2021 03:24
-
-
Save choco0908/891287af802e10ade5ce14de04f23204 to your computer and use it in GitHub Desktop.
NSAttributedString_Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef NSAttributedString_Extension_h | |
#define NSAttributedString_Extension_h | |
NS_ASSUME_NONNULL_BEGIN | |
@interface NSAttributedString (Extension) | |
+ (NSAttributedString *)stringWithFormat:(NSAttributedString *)format, ... NS_REQUIRES_NIL_TERMINATION; | |
@end | |
NS_ASSUME_NONNULL_END | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSAttributedString+Extension.h" | |
@implementation NSAttributedString (Extension) | |
+ (NSAttributedString *)stringWithFormat:(NSAttributedString *)format, ...{ | |
va_list args; | |
va_start(args, format); | |
NSMutableAttributedString *mutableAttributedString = (NSMutableAttributedString*)[format mutableCopy]; | |
NSString *mutableString = [mutableAttributedString string]; | |
while (true) { | |
NSAttributedString *arg = va_arg(args, NSAttributedString *); | |
if(!arg) { | |
break; | |
} | |
NSRange rangeOfStringToBeReplaced = [mutableString rangeOfString:@"%@"]; | |
[mutableAttributedString replaceCharactersInRange:rangeOfStringToBeReplaced withAttributedString:arg]; | |
} | |
va_end(args); | |
return mutableAttributedString; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment