Created
May 5, 2014 20:28
-
-
Save beccadax/11546648 to your computer and use it in GitHub Desktop.
Do simple +stringWithFormat:-type operations with NSAttributedString.
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
// | |
// NSAttributedString+format.m | |
// Chatterbox | |
// | |
// Created by Brent Royal-Gordon on 2/7/14. | |
// Copyright (c) 2014 Architechies. All rights reserved. | |
// | |
#import "NSAttributedString+format.h" | |
@implementation NSAttributedString (format) | |
- (instancetype)initWithFormat:(NSString *)format attributes:(NSDictionary *)attrs argumentAttributes:(NSDictionary *)argAttrs arguments:(va_list)argList { | |
NSMutableAttributedString * text = [[NSMutableAttributedString alloc] initWithString:@"" attributes:attrs]; | |
NSScanner * scanner = [NSScanner scannerWithString:format]; | |
scanner.charactersToBeSkipped = [NSCharacterSet new]; | |
while (![scanner isAtEnd]) { | |
NSString * discarded; | |
[scanner scanUpToString:@"%" intoString:&discarded]; | |
if(discarded) { | |
[text appendAttributedString:[[NSAttributedString alloc] initWithString:discarded attributes:attrs]]; | |
} | |
if([scanner scanString:@"%%" intoString:NULL]) { | |
[text appendAttributedString:[[NSAttributedString alloc] initWithString:@"%" attributes:attrs]]; | |
} | |
else if([scanner scanString:@"%@" intoString:NULL]) { | |
id object = va_arg(argList, id); | |
if(![object isKindOfClass:NSAttributedString.class]) { | |
object = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", object] attributes:argAttrs]; | |
} | |
[text appendAttributedString:object]; | |
} | |
else if([scanner scanString:@"%" intoString:NULL]) { | |
NSString * specifier = [format substringFromIndex:scanner.scanLocation]; | |
if(specifier.length > 1) { | |
specifier = [specifier substringToIndex:1]; | |
} | |
NSAssert(NO, @"Unsupported format specifier '%@'", specifier); | |
} | |
} | |
return text; | |
} | |
- (instancetype)initWithFormat:(NSString *)format attributes:(NSDictionary *)attrs argumentAttributes:(NSDictionary *)argAttrs, ... { | |
va_list args; | |
va_start(args, argAttrs); | |
NSAttributedString * text = [self initWithFormat:format attributes:attrs argumentAttributes:argAttrs arguments:args]; | |
va_end(args); | |
return text; | |
} | |
@end |
Really like the concept, but having an issue va_arg(argList, id) going to nil on the second object, is NSDictionary correct? Is there an example that implements this category?
Hello @brentax, is it allowed to use this gist in source code of a commercial application?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice!
My approach to this was a constructor with a regexp,
%(\d+)\$@
, and an array of attributed strings to interpolate.