Last active
March 23, 2017 15:13
-
-
Save anzfactory/1ca89c68e04262eb1c0799cda4ab8f5d to your computer and use it in GitHub Desktop.
NSAttributedStringをちょっとは使いやすくするやーつ
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
@interface ANZTextStyle : NSObject | |
@property (nonatomic, readonly) ANZTextStyle *(^text)(NSString *text); | |
@property (nonatomic, readonly) ANZTextStyle *(^lineHeight)(CGFloat height); | |
@property (nonatomic, readonly) ANZTextStyle *(^breakMode)(NSLineBreakMode mode); | |
@property (nonatomic, readonly) ANZTextStyle *(^align)(NSTextAlignment align); | |
@property (nonatomic, readonly) ANZTextStyle *(^color)(UIColor *color); | |
@property (nonatomic, readonly) ANZTextStyle *(^font)(UIFont *font); | |
@property (nonatomic, readonly) ANZTextStyle *(^bgColor)(UIColor *color); | |
@property (nonatomic, readonly) ANZTextStyle *(^baseline)(CGFloat offset); | |
@property (nonatomic, readonly) ANZTextStyle *(^underline)(); | |
@property (nonatomic, readonly) ANZTextStyle *(^kern)(CGFloat value); | |
@property (nonatomic, readonly) ANZTextStyle *(^range)(NSInteger offset, NSInteger length); | |
- (instancetype)initWithString:(NSString *)string; | |
- (NSAttributedString *)build; | |
- (void)clean; | |
@end | |
@interface NSString (ANZ_TextStyleExtension) | |
- (ANZTextStyle *)style; | |
@end |
This file contains hidden or 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
#import "ANZTextStyle.h" | |
static NSString * const kParagraphKeyLineHeight = @"height"; | |
static NSString * const kParagraphKeyAlignment = @"alignment"; | |
static NSString * const kParagraphKeyBreakMode = @"breakmode"; | |
@interface ANZTextStyle() | |
@property (nonatomic) NSMutableDictionary *attributes; | |
@property (nonatomic) NSMutableDictionary *paragraph; | |
@property (nonatomic) NSString *string; | |
@property (nonatomic) NSRange applyRange; | |
@end | |
@implementation ANZTextStyle | |
- (instancetype)initWithString:(NSString *)string { | |
if (self = [super init]) { | |
_string = string; | |
_attributes = [NSMutableDictionary new]; | |
_paragraph = [NSMutableDictionary new]; | |
} | |
return self; | |
} | |
#pragma mark - getter | |
- (ANZTextStyle *(^)(NSString *))text { | |
return ^ANZTextStyle *(NSString *text) { | |
self.string = text; | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(CGFloat))lineHeight { | |
return ^ANZTextStyle *(CGFloat height) { | |
self.paragraph[kParagraphKeyLineHeight] = @(height); | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(NSLineBreakMode))breakMode { | |
return ^ANZTextStyle *(NSLineBreakMode mode) { | |
self.paragraph[kParagraphKeyBreakMode] = @(mode); | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(NSTextAlignment))align { | |
return ^ANZTextStyle *(NSTextAlignment align) { | |
self.paragraph[kParagraphKeyAlignment] = @(align); | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(UIColor *))color { | |
return ^ANZTextStyle *(UIColor *color) { | |
self.attributes[NSForegroundColorAttributeName] = color; | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(UIFont *))font { | |
return ^ANZTextStyle *(UIFont *font) { | |
self.attributes[NSFontAttributeName] = font; | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(UIColor *))bgColor { | |
return ^ANZTextStyle *(UIColor *bgColor) { | |
self.attributes[NSBackgroundColorAttributeName] = bgColor; | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(CGFloat))baseline { | |
return ^ANZTextStyle *(CGFloat baseline) { | |
self.attributes[NSBaselineOffsetAttributeName] = @(baseline); | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)())underline { | |
return ^ANZTextStyle *() { | |
self.attributes[NSUnderlineStyleAttributeName] = @(NSUnderlineStyleSingle); | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(CGFloat))kern { | |
return ^ANZTextStyle *(CGFloat value) { | |
self.attributes[NSKernAttributeName] = @(value); | |
return self; | |
}; | |
} | |
- (ANZTextStyle *(^)(NSInteger offset, NSInteger length))range { | |
return ^ANZTextStyle *(NSInteger offset, NSInteger length) { | |
self.applyRange = NSMakeRange(offset, length); | |
return self; | |
}; | |
} | |
#pragma mark - setter | |
- (void)setString:(NSString *)string { | |
_string = string; | |
self.applyRange = NSMakeRange(0, 0); // string変わったらrangeはクリアする | |
} | |
#pragma mark - method | |
- (NSAttributedString *)build { | |
NSAssert(self.string.length, @"string is empty!!!!"); | |
NSParagraphStyle *paragraphStyle = [self makeParagraph]; | |
if (paragraphStyle) { | |
self.attributes[NSParagraphStyleAttributeName] = paragraphStyle; | |
} | |
if (self.applyRange.length) { | |
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:self.string]; | |
[attributeString addAttributes:self.attributes range:self.applyRange]; | |
return [[NSAttributedString alloc] initWithAttributedString: attributeString]; | |
} else { | |
return [[NSAttributedString alloc] initWithString:self.string attributes:self.attributes]; | |
} | |
} | |
- (void)clean { | |
self.attributes = [NSMutableDictionary new]; | |
self.paragraph = [NSMutableDictionary new]; | |
self.applyRange = NSMakeRange(0, 0); | |
} | |
- (NSParagraphStyle *)makeParagraph { | |
if (![self.paragraph allKeys].count) { | |
return nil; | |
} | |
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; | |
NSNumber *value = self.paragraph[kParagraphKeyLineHeight]; | |
if (value) { | |
style.maximumLineHeight = style.minimumLineHeight = value.floatValue; | |
} | |
value = self.paragraph[kParagraphKeyAlignment]; | |
if (value) { | |
style.alignment = (NSTextAlignment)value.integerValue; | |
} | |
value = self.paragraph[kParagraphKeyBreakMode]; | |
if (value) { | |
style.lineBreakMode = (NSLineBreakMode)value.integerValue; | |
} | |
return style; | |
} | |
@end | |
@implementation NSString(ANZ_TextStyleExtension) | |
- (ANZTextStyle *)style { | |
return [[ANZTextStyle alloc] initWithString:self]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment