UILabel * label = [[UILabel alloc] initWithFrame:CGRectZero addToView:self.view];
label.backgroundColor = [UIColor redColor];
label.font = [UIFont systemFontOfSize:16];
label.text = @"dfadfadfadfadfjka;fka;dfja";
label.layer.borderColor = [UIColor yellowColor].CGColor;
label.layer.borderWidth = 1;
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self.view);
}];
前些日子写项目 UI图都出来了 那就做呗 突然看见一个界面上面 有78个标签 出来内容不一样 其他的都是一样的 什么 ? 难道让我把上面的代码复制78份开什么鬼玩笑 我不要!!! 怎么办呢 想办法呗 心情一不好就写了个工具 UIView+FunctionExtension.h
@interface UIView (StyleFormat)
/// 对象最好为同一条继承链上面的实例
/// 共享样式
@property (nonatomic, copy) void (^ shareStyle) (id sourceView);
/// 独有样式
@property (nonatomic, copy) void (^ uniqueStyle) (id sourceView);
// 有
- (void)shareStyle:(void(^)(id sourceView))share uniqueStyle:(void(^)(id sourceView))unique;
// 无循环引用问题
- (void)configureStyle:(void(^)(id sourceView))style;
@end
UIView+FunctionExtension.m
@implementation UIView (StyleFormat)
- (void)setShareStyle:(void (^)(id))shareStyle {
if (shareStyle != nil) {
shareStyle(self);
}
objc_setAssociatedObject(self, @selector(shareStyle), shareStyle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(id))shareStyle {
return objc_getAssociatedObject(self, _cmd);
}
- (void)setUniqueStyle:(void (^)(id))uniqueStyle {
if (uniqueStyle != nil) {
uniqueStyle(self);
}
objc_setAssociatedObject(self, @selector(uniqueStyle), uniqueStyle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(id))uniqueStyle {
return objc_getAssociatedObject(self, _cmd);
}
- (void)shareStyle:(void(^)(id sourceView))share uniqueStyle:(void(^)(id sourceView))unique {
self.shareStyle = share;
self.uniqueStyle = unique;
}
+ (BOOL)resolveInstanceMethod:(SEL)sel {
NSString * selName = NSStringFromSelector(sel);
if ([selName hasPrefix:@"set"] == YES) {
class_addMethod([self class], sel, (IMP)realizationFunction, "v@:");
return YES;
} else {
return [super resolveInstanceMethod:sel];
}
}
void realizationFunction(id obj, SEL _cmd) {
// NSLog(@"%@:%@", obj, NSStringFromSelector(_cmd));
}
- (void)configureStyle:(void(^)(id sourceView))style {
if (style != nil) {
style(self);
}
}
@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;
是不是比复制一份好的多呢 喜欢的话点个赞呗 各位看官老爷们 附上CSDN的链接 http://blog.csdn.net/betrayalpromise/article/details/78790916