Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kccheung/ab8adedca3ec621d64ea to your computer and use it in GitHub Desktop.
Save kccheung/ab8adedca3ec621d64ea to your computer and use it in GitHub Desktop.
//
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment