iOS 视图样式 该怎么样复用

来源:互联网 发布:淘宝彩票恢复 编辑:程序博客网 时间:2024/05/02 02:39

其实也希望能像css那样处理iOS程序

UILabel * label = [[UILabel alloc]initWithFrame:CGRectZero addToView:self.view];

label.backgroundColor = [UIColorredColor];

label.font = [UIFontsystemFontOfSize:16];

label.text =@"dfadfadfadfadfjka;fka;dfja";

label.layer.borderColor = [UIColoryellowColor].CGColor;

label.layer.borderWidth =1;

[label mas_makeConstraints:^(MASConstraintMaker *make) {

make.center.mas_equalTo(self.view);

}];


这种代码写的太多了 没有多少意思 思考一段时间了 觉得应该有办法的 我的办法是使用闭包

第一版 

UIView+FunctionExtension.h

@interface UIView (StyleFormat)


/// 只支持同类型的实例


/// 共享样式

@property (nonatomic, copy) void (^ _Nullable shareStyle) (id_Nonnull sourceView);

/// 独有样式

@property (nonatomic, copy) void (^ _Nullable uniqueStyle) (id_Nonnull sourceView);


- (void)shareStyle:(void(^_Nullable)(id_Nonnull sourceView))share uniqueStyle:(void(^_Nullable)(id_Nonnull sourceView))unique;


@end

UIView+FunctionExtension.m

@implementation UIView (StyleFormat)


- (void)setShareStyle:(void (^_Nullable)(id_Nonnull sourceView))shareStyle {

    if (shareStyle != nil) {

        shareStyle(self);

    }

    objc_setAssociatedObject(self,@selector(shareStyle), shareStyle,OBJC_ASSOCIATION_COPY_NONATOMIC);

}


- (void (^ _Nullable)(id_Nonnull sourceView))shareStyle {

    return objc_getAssociatedObject(self,_cmd);

}


- (void)setUniqueStyle:(void (^_Nullable)(id_Nonnull sourceView))uniqueStyle {

    if (uniqueStyle != nil) {

        uniqueStyle(self);

    }

    objc_setAssociatedObject(self,@selector(uniqueStyle), uniqueStyle,OBJC_ASSOCIATION_COPY_NONATOMIC);

}


- (void (^ _Nullable)(id_Nonnull sourceView))uniqueStyle {

    return objc_getAssociatedObject(self,_cmd);

}


- (void)shareStyle:(void(^_Nullable)(id_Nonnull sourceView))share uniqueStyle:(void(^_Nullable)(id_Nonnull sourceView))unique {

    self.shareStyle  = share;

    self.uniqueStyle = unique;

}


@end




想之前的代码可以化简为

UILabel * label0 = [[UILabel alloc] initWithFrame:CGRectZero addToView:self.view];

[label0 shareStyle:^(UILabel * sourceView) {

sourceView.backgroundColor = [UIColor redColor];

        sourceView.font = [UIFont systemFontOfSize:16];

        sourceView.layer.borderColor = [UIColor yellowColor].CGColor;

        sourceView.layer.borderWidth = 1;

} uniqueStyle:^(UILabel * sourceView) {

        sourceView.text = @"dfadfadfadfadfjka;fka;dfja";

}];

[label0 mas_makeConstraints:^(MASConstraintMaker *make) {

make.center.mas_equalTo(self.view);

}];


UILabel * label1 = [[UILabel alloc] initWithFrame:CGRectZero addToView:self.view];

UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectZero addToView:self.view];

label1.shareStyle = label0.shareStyle;

label2.shareStyle = label0.shareStyle;

看起来效果不错 但是目前还正在考虑继承系统的样式处理 有注意的伙伴不吝赐教

第二版

UIView+FunctionExtension.h

@interface UIView (StyleFormat)


/// 只支持同一条继承链上面的实例


/// 共享样式

@property (nonatomic, copy) void (^ _Nullable shareStyle) (id_Nonnull sourceView);

/// 独有样式

@property (nonatomic, copy) void (^ _Nullable uniqueStyle) (id_Nonnull sourceView);


- (void)shareStyle:(void(^_Nullable)(id_Nonnull sourceView))share uniqueStyle:(void(^_Nullable)(id_Nonnull sourceView))unique;


@end


UIView+FunctionExtension.m

@implementation UIView (StyleFormat)


- (void)setShareStyle:(void (^_Nullable)(id_Nonnull sourceView))shareStyle {

    if (shareStyle != nil) {

        shareStyle(self);

    }

    objc_setAssociatedObject(self,@selector(shareStyle), shareStyle,OBJC_ASSOCIATION_COPY_NONATOMIC);

}


- (void (^ _Nullable)(id_Nonnull sourceView))shareStyle {

    return objc_getAssociatedObject(self,_cmd);

}


- (void)setUniqueStyle:(void (^_Nullable)(id_Nonnull sourceView))uniqueStyle {

    if (uniqueStyle != nil) {

        uniqueStyle(self);

    }

    objc_setAssociatedObject(self,@selector(uniqueStyle), uniqueStyle,OBJC_ASSOCIATION_COPY_NONATOMIC);

}


- (void (^ _Nullable)(id_Nonnull sourceView))uniqueStyle {

    return objc_getAssociatedObject(self,_cmd);

}



- (void)shareStyle:(void(^_Nullable)(id_Nonnull sourceView))share uniqueStyle:(void(^_Nullable)(id_Nonnull sourceView))unique {

    self.shareStyle  = share;

    self.uniqueStyle = unique;

}



- (id)forwardingTargetForSelector:(SEL)aSelector {

    return [MessageObject new];

}


@end

MessageObject.h

@interface MessageObject : NSObject


@end

MessageObject.m

void realizationFunction(id obj,SEL_cmd);


@implementation MessageObject


+ (BOOL)resolveInstanceMethod:(SEL)sel {

    if (![self respondsToSelector:sel]) {

        class_addMethod([self class], sel, (IMP)realizationFunction,"v@:");

    }

    return YES;

}


@end


void realizationFunction(id obj,SEL_cmd) {

    NSLog(@"%@:%@", obj,NSStringFromSelector(_cmd));

}

目前除了打印有些不一样 是否有BUG进一步验证

2017-12-13 23:09:42.894667+0800 Layout[66916:2018402] (null):encodeWithOSLogCoder:options:maxLength:

2017-12-13 23:09:42.894907+0800 Layout[66916:2018402] (null):encodeWithOSLogCoder:options:maxLength:

2017-12-13 23:09:42.895193+0800 Layout[66916:2018402] (null):encodeWithOSLogCoder:options:maxLength:




阅读全文
0 0
原创粉丝点击