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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容