Skip to content

Instantly share code, notes, and snippets.

@keithnorm
Last active August 29, 2015 14:05
Show Gist options
  • Save keithnorm/594c70831f8be9b21056 to your computer and use it in GitHub Desktop.
Save keithnorm/594c70831f8be9b21056 to your computer and use it in GitHub Desktop.
A category to allow setting dynamic type via Classy
UILabel {
font-text-style: text-style-headline
}
#import <UIKit/UIKit.h>
@interface UILabel (StyleExtensions)
@property (nonatomic, strong) NSString *fontTextStyle;
@end
#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