Created
October 22, 2015 09:08
-
-
Save cyrilchandelier/3490de97193ae6783798 to your computer and use it in GitHub Desktop.
Stylesheet like IBInspectable labels
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+Stylesheet.h | |
// Stylesheet | |
// | |
// Created by Cyril Chandelier on 21/10/15. | |
// Copyright © 2015 CyrilChandelier. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UILabel (Stylesheet) | |
+ (void)registerStyleNamed:(nonnull NSString *)name block:(nonnull void(^)(NSString * _Nonnull, NSMutableAttributedString * _Nonnull , NSMutableParagraphStyle * _Nonnull))block; | |
@property (nonatomic, strong, nonnull) IBInspectable NSString *styles; | |
@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
// | |
// UILabel+Stylesheet.m | |
// Stylesheet | |
// | |
// Created by Cyril Chandelier on 21/10/15. | |
// Copyright © 2015 CyrilChandelier. All rights reserved. | |
// | |
#import "UILabel+Stylesheet.h" | |
#import <objc/runtime.h> | |
@implementation UILabel (Stylesheet) | |
- (void)awakeFromNib | |
{ | |
[super awakeFromNib]; | |
[self refresh]; | |
} | |
- (void)refresh | |
{ | |
NSRange globalRange = NSMakeRange(0, self.text.length); | |
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; | |
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:self.text]; | |
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:globalRange]; | |
for (NSString *style in [self.styles componentsSeparatedByString:@" "]) | |
{ | |
void(^block)(NSString *, NSMutableAttributedString *, NSMutableParagraphStyle *) = [[self class] registeredStyles][style]; | |
block(self.text, attributedText, paragraphStyle); | |
} | |
self.attributedText = attributedText; | |
} | |
#pragma mark - Style registration | |
+ (void)registerStyleNamed:(nonnull NSString *)name block:(nonnull void(^)(NSString * _Nonnull, NSMutableAttributedString * _Nonnull , NSMutableParagraphStyle * _Nonnull))block | |
{ | |
[[[self class] registeredStyles] setObject:[block copy] forKey:name]; | |
} | |
#pragma mark - Private getters and setters | |
static void * stylesKey = &stylesKey; | |
static void * registeredStylesKey = ®isteredStylesKey; | |
- (NSString *)styles | |
{ | |
return objc_getAssociatedObject(self, &stylesKey); | |
} | |
- (void)setStyles:(NSString *)styles | |
{ | |
objc_setAssociatedObject(self, &stylesKey, styles, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
[self refresh]; | |
} | |
+ (NSMutableDictionary *)registeredStyles | |
{ | |
NSMutableDictionary *registeredStyles = objc_getAssociatedObject(self, ®isteredStylesKey); | |
if (!registeredStyles) | |
{ | |
registeredStyles = [NSMutableDictionary dictionary]; | |
objc_setAssociatedObject(self, ®isteredStylesKey, registeredStyles, OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
} | |
return registeredStyles; | |
} | |
@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+Stylesheet.h" | |
[UILabel registerStyleNamed:@"title" block:^(NSString *text, NSMutableAttributedString *attributedText, NSMutableParagraphStyle *paragraphStyle) { | |
[attributedText addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:30.0f] range:NSMakeRange(0, [text length])]; | |
}]; | |
[UILabel registerStyleNamed:@"content" block:^(NSString *text, NSMutableAttributedString *attributedText, NSMutableParagraphStyle *paragraphStyle) { | |
[attributedText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22.0f] range:NSMakeRange(0, [text length])]; | |
}]; | |
[UILabel registerStyleNamed:@"color" block:^(NSString *text, NSMutableAttributedString *attributedText, NSMutableParagraphStyle *paragraphStyle) { | |
[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, [text length])]; | |
}]; | |
[UILabel registerStyleNamed:@"center" block:^(NSString *text, NSMutableAttributedString *attributedText, NSMutableParagraphStyle *paragraphStyle) { | |
paragraphStyle.alignment = NSTextAlignmentCenter; | |
}]; | |
[UILabel registerStyleNamed:@"red" block:^(NSString *text, NSMutableAttributedString *attributedText, NSMutableParagraphStyle *paragraphStyle) { | |
[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [text length])]; | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment