Skip to content

Instantly share code, notes, and snippets.

@cyrilchandelier
Created October 22, 2015 09:08
Show Gist options
  • Save cyrilchandelier/3490de97193ae6783798 to your computer and use it in GitHub Desktop.
Save cyrilchandelier/3490de97193ae6783798 to your computer and use it in GitHub Desktop.
Stylesheet like IBInspectable labels
//
// 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
//
// 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 = &registeredStylesKey;
- (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, &registeredStylesKey);
if (!registeredStyles)
{
registeredStyles = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &registeredStylesKey, registeredStyles, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return registeredStyles;
}
@end
#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