Objective-c Category 添加属性

@interface UIView (JFResourceService)
@property(copy,nonatomic) NSString *jf_style;
@end


@interface UILabel (JFResourceService)
@property(copy,nonatomic) NSString *jf_resource;
@end

@interface UIButton (JFResourceService)
@property(copy,nonatomic) NSString *jf_resource;
@end


#import "UIView+JFResourceService.h"
#import 
#import "JFViewStyle.h"

@implementation UIView (JFResourceService)
-(void)setJf_style:(NSString *)jf_style{
    id services = [self jf_objection:@protocol(JFResourceServiceProtocol)];
    JFViewStyle *style = [services styleForKey:jf_style];
    [style applyToView:self];
    objc_setAssociatedObject(self, @selector(jf_style), jf_style, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

-(NSString *)jf_style{
    return objc_getAssociatedObject(self, _cmd);
}
@end

@implementation UILabel (JFResourceService)
-(void)setJf_resource:(NSString *)jf_resource{
    
    NSString *key = [jf_resource componentsSeparatedByString:@"@"].lastObject;
    
    id services = [self jf_objection:@protocol(JFResourceServiceProtocol)];
    self.text = [services stringForKey:[NSString stringWithFormat:@"%@@%@",@"string",key]];
    self.textColor = [services colorForKey:[NSString stringWithFormat:@"%@@%@",@"color",key]];
    self.font = [services fontForKey:[NSString stringWithFormat:@"%@@%@",@"font",key]];
    objc_setAssociatedObject(self, @selector(jf_resource), jf_resource, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

-(NSString *)jf_resource{
    return objc_getAssociatedObject(self, _cmd);
}
@end

@implementation UIButton (JFResourceService)
-(void)setJf_resource:(NSString *)jf_resource{
   NSString *key = [jf_resource componentsSeparatedByString:@"@"].lastObject;
    id services = [self jf_objection:@protocol(JFResourceServiceProtocol)];
    [self setTitle:[services stringForKey:[NSString stringWithFormat:@"%@@%@",@"string",key]] forState:UIControlStateNormal];
    [self setTitleColor:[services colorForKey:[NSString stringWithFormat:@"%@@%@",@"color",key]] forState:UIControlStateNormal];
    self.titleLabel.font = [services fontForKey:[NSString stringWithFormat:@"%@@%@",@"font",key]];
    objc_setAssociatedObject(self, @selector(jf_resource), jf_resource, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

-(NSString *)jf_resource{
    return objc_getAssociatedObject(self, _cmd);
}
@end

你可能感兴趣的:(Objective-c Category 添加属性)