Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Janak-Nirmal/9543492 to your computer and use it in GitHub Desktop.
Save Janak-Nirmal/9543492 to your computer and use it in GitHub Desktop.
//
// NSAttributedString+RZExtensions.h
// Raizlabs
//
// Created by Alex Rouse on 12/13/13.
// Copyright 2014 Raizlabs and other contributors
// http://raizlabs.com/
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import <Foundation/Foundation.h>
@interface NSAttributedString (RZExtensions)
// Provide a list of strings followed by attributes and returns a NSAttributedString for their combination.
//
// Example:
// [NSAttributedString attributedStringWithStringsAndAttributes:@"test",attributes,@"test2",attributes2,nil];
//
+ (instancetype)rz_attributedStringWithStringsAndAttributes:(id)firstString, ... NS_REQUIRES_NIL_TERMINATION;
@end
//
// NSAttributedString+RZExtensions.m
// Raizlabs
//
// Created by Alex Rouse on 12/13/13.
// Copyright 2014 Raizlabs and other contributors
// http://raizlabs.com/
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "NSAttributedString+RZExtensions.h"
@implementation NSAttributedString (RZExtensions)
+ (instancetype)rz_attributedStringWithStringsAndAttributes:(id)firstString, ... NS_REQUIRES_NIL_TERMINATION
{
NSMutableAttributedString* string = [[NSMutableAttributedString alloc] init];
id param;
NSDictionary* attributes;
NSString* content = firstString;
va_list argumentList;
if (content)
{
va_start(argumentList, firstString);
while ((param = va_arg(argumentList, id)) != nil)
{
if ([param isKindOfClass:[NSString class]])
{
content = param;
}
else if ([param isKindOfClass:[NSDictionary class]])
{
attributes = param;
}
if ([content isKindOfClass:[NSString class]] && [attributes isKindOfClass:[NSDictionary class]] )
{
NSAttributedString* str = [[NSAttributedString alloc] initWithString:content attributes:attributes];
[string appendAttributedString:str];
content = nil;
attributes = nil;
}
}
va_end(argumentList);
}
return [[NSAttributedString alloc] initWithAttributedString:string];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment