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 <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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容