其实也希望能像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;
}
- (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:
异常的日志我的项目用到现在没有什么问题
工具链接 https://github.com/BetrayalPromise/MineCommonUtils 希望各位大佬赏赐一颗start