Sha256: a5a67e49a04080b9d4633b6d425219ae76a360a63935ae210129e2eb71199a75

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

//
//  NSAttributedString+CCLFormat.m
//  Cocode
//
//  Created by Kyle Fuller on 06/11/2012.
//  Copyright (c) 2012-2014 Cocode. All rights reserved.
//

#import "NSAttributedString+CCLFormat.h"

@implementation NSAttributedString (CCLFormat)

+ (NSAttributedString *)attributedStringWithFormat:(NSString *)format, ... {
    NSMutableArray *attributes = [NSMutableArray array];

    va_list args;
    va_start(args, format);

    NSString *string = [format stringByReplacingOccurrencesOfString:@"%@" withString:@""];
    NSUInteger count = ([format length] - [string length]) / [@"%@" length];

    for (NSUInteger index = 0; index < count; index++) {
        id argument = va_arg(args, id);
        [attributes addObject:argument];
    }
    va_end(args);

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:format];
    [attributedString beginEditing];

    NSRange range = [format rangeOfString:@"%@" options:NSBackwardsSearch];
    for (NSUInteger index = 0; range.location != NSNotFound; index++) {
        id attribute = [attributes lastObject];
        [attributes removeLastObject];

        if ([attribute isKindOfClass:[NSAttributedString class]]) {
            [attributedString replaceCharactersInRange:range withAttributedString:attribute];
        } else {
            [attributedString replaceCharactersInRange:range withString:[attribute description]];
        }

        range = NSMakeRange(0, range.location);
        range = [format rangeOfString:@"%@" options:NSBackwardsSearch range:range];
    }

    [attributedString endEditing];
    return [attributedString copy];
}

@end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cocoapods-deintegrate-0.2.1 spec/fixtures/Project/StaticLibraries/Pods/NSAttributedString+CCLFormat/NSAttributedString+CCLFormat.m
cocoapods-deintegrate-0.2.0 spec/fixtures/Project/StaticLibraries/Pods/NSAttributedString+CCLFormat/NSAttributedString+CCLFormat.m