美文网首页
Objective-c Category 添加属性

Objective-c Category 添加属性

作者: 楼上那位 | 来源:发表于2016-11-01 16:44 被阅读31次
@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 <objc/runtime.h>
#import "JFViewStyle.h"

@implementation UIView (JFResourceService)
-(void)setJf_style:(NSString *)jf_style{
    id<JFResourceServiceProtocol> 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<JFResourceServiceProtocol> 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<JFResourceServiceProtocol> 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 添加属性

      本文链接:https://www.haomeiwen.com/subject/nxbmuttx.html