Last active
August 29, 2015 14:05
-
-
Save keithnorm/594c70831f8be9b21056 to your computer and use it in GitHub Desktop.
A category to allow setting dynamic type via Classy
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
UILabel { | |
font-text-style: text-style-headline | |
} |
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
#import <UIKit/UIKit.h> | |
@interface UILabel (StyleExtensions) | |
@property (nonatomic, strong) NSString *fontTextStyle; | |
@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
#import "UILabel+StyleExtensions.h" | |
#import <Classy/Classy.h> | |
#import <objc/runtime.h> | |
@implementation UILabel (StyleExtensions) | |
- (NSString *)fontTextStyle { | |
return objc_getAssociatedObject(self, @selector(fontTextStyle)); | |
} | |
- (void)setFontTextStyle:(NSString *)fontTextStyle { | |
NSDictionary *textStyleMap = @{ | |
@"text-style-headline": UIFontTextStyleHeadline, | |
@"text-style-subheadline": UIFontTextStyleSubheadline, | |
@"text-style-body": UIFontTextStyleBody, | |
@"text-style-footnote": UIFontTextStyleFootnote, | |
@"text-style-caption1": UIFontTextStyleCaption1, | |
@"text-style-caption2": UIFontTextStyleCaption2 | |
}; | |
if ([textStyleMap valueForKey:fontTextStyle]) { | |
objc_setAssociatedObject(self, @selector(fontTextStyle), [textStyleMap valueForKey:fontTextStyle], OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
self.font = [UIFont preferredFontForTextStyle:[textStyleMap valueForKey:fontTextStyle]]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment