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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容