Created
September 25, 2013 10:46
-
-
Save shaps80/6697953 to your computer and use it in GitHub Desktop.
NSAttributedString Category to add more flexible customisation.
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
/* | |
Copyright (c) 2013 Snippex. All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY Snippex `AS IS' AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
EVENT SHALL Snippex OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | |
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#import <Foundation/Foundation.h> | |
@interface NSAttributedString (SPXAdditions) | |
+ (NSAttributedString *)attributedString:(NSString *)string; | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
attributes:(NSDictionary *)attributes; | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
color:(UIColor *)color | |
font:(UIFont *)font; | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
color:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment; | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
color:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight; | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
color:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight | |
kerning:(CGFloat)kerning; | |
- (NSAttributedString *)attributedStringWithUpdatedAttributes:(NSDictionary *)attributes | |
range:(NSRange)range; | |
+ (NSDictionary *)attributesForColor:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight | |
kerning:(CGFloat)kerning; | |
@end | |
@interface NSMutableAttributedString (SPXAdditions) | |
- (void)updateWithColor:(UIColor *)color | |
font:(UIFont *)font; | |
- (void)updateWithColor:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment; | |
- (void)updateWithColor:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight; | |
- (void)updateWithColor:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight | |
kerning:(CGFloat)kerning; | |
@end |
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
/* | |
Copyright (c) 2013 Snippex. All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this | |
list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY Snippex `AS IS' AND ANY EXPRESS OR | |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
EVENT SHALL Snippex OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | |
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
#import "NSAttributedString+SPXAdditions.h" | |
@implementation NSAttributedString (SPXAdditions) | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
{ | |
return [[NSAttributedString alloc] initWithString:string]; | |
} | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
attributes:(NSDictionary *)attributes | |
{ | |
return [[NSAttributedString alloc] initWithString:string attributes:attributes]; | |
} | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
color:(UIColor *)color | |
font:(UIFont *)font | |
{ | |
return [self attributedString:string color:color font:font alignment:NSTextAlignmentLeft lineHeight:0 kerning:0]; | |
} | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
color:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
{ | |
return [self attributedString:string color:color font:font alignment:alignment lineHeight:0 kerning:0]; | |
} | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
color:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight | |
{ | |
return [self attributedString:string color:color font:font alignment:alignment lineHeight:lineHeight kerning:0]; | |
} | |
+ (NSAttributedString *)attributedString:(NSString *)string | |
color:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight | |
kerning:(CGFloat)kerning | |
{ | |
NSDictionary *attributes = [self attributesForColor:color font:font alignment:alignment lineHeight:lineHeight kerning:kerning]; | |
return [[NSAttributedString alloc] initWithString:string attributes:attributes]; | |
} | |
- (NSAttributedString *)attributedStringWithUpdatedAttributes:(NSDictionary *)attributes range:(NSRange)range | |
{ | |
NSMutableAttributedString *string = [self mutableCopy]; | |
[string addAttributes:attributes range:range]; | |
return [string copy]; | |
} | |
+ (NSDictionary *)attributesForColor:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight | |
kerning:(CGFloat)kerning | |
{ | |
NSMutableParagraphStyle *style = [self sharedParagraphStyle]; | |
style.lineSpacing = lineHeight; | |
return @{ NSFontAttributeName : font ?: [UIFont systemFontOfSize:18], | |
NSForegroundColorAttributeName : color ?: [UIColor blackColor], | |
NSParagraphStyleAttributeName : style, | |
NSKernAttributeName : @(kerning), | |
}; | |
} | |
+ (NSMutableParagraphStyle *)sharedParagraphStyle | |
{ | |
static NSMutableParagraphStyle *style = nil; | |
if (!style) style = [[NSMutableParagraphStyle alloc] init]; | |
return style; | |
} | |
@end | |
@implementation NSMutableAttributedString (SPXAdditions) | |
- (void)updateWithColor:(UIColor *)color | |
font:(UIFont *)font | |
{ | |
NSDictionary *attributes = [self.class attributesForColor:color font:font alignment:NSTextAlignmentLeft lineHeight:0 kerning:0]; | |
return [self addAttributes:attributes range:NSMakeRange(0, self.length)]; | |
} | |
- (void)updateWithColor:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
{ | |
NSDictionary *attributes = [self.class attributesForColor:color font:font alignment:alignment lineHeight:0 kerning:0]; | |
return [self addAttributes:attributes range:NSMakeRange(0, self.length)]; | |
} | |
- (void)updateWithColor:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight | |
{ | |
NSDictionary *attributes = [self.class attributesForColor:color font:font alignment:alignment lineHeight:lineHeight kerning:0]; | |
return [self addAttributes:attributes range:NSMakeRange(0, self.length)]; | |
} | |
- (void)updateWithColor:(UIColor *)color | |
font:(UIFont *)font | |
alignment:(NSTextAlignment)alignment | |
lineHeight:(CGFloat)lineHeight | |
kerning:(CGFloat)kerning | |
{ | |
NSDictionary *attributes = [self.class attributesForColor:color font:font alignment:alignment lineHeight:lineHeight kerning:kerning]; | |
return [self addAttributes:attributes range:NSMakeRange(0, self.length)]; | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment