Skip to content

Instantly share code, notes, and snippets.

@choco0908
Created May 24, 2021 03:24
Show Gist options
  • Save choco0908/891287af802e10ade5ce14de04f23204 to your computer and use it in GitHub Desktop.
Save choco0908/891287af802e10ade5ce14de04f23204 to your computer and use it in GitHub Desktop.
NSAttributedString_Extension
#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
#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