A category for UIView to copy and reuse.
Core:
+ (__kindof UIView*)duplicate:(__kindof UIView*)view
{
NSData * tempArchive = [NSKeyedArchiver archivedDataWithRootObject:view];
return [NSKeyedUnarchiver unarchiveObjectWithData:tempArchive];
}
Than, we can use - (__kindof UIView*)clone;
to copy UIIView
- (UIView *)clone
{
return [[self class] duplicate:self];
}
We can set key, and use make block
to set style of this view.
+ (void)registStyle:(NSString*)key make:(void(^)(id view))makeBlock;
for example :
[MDCButton registStyle:@"category_button" make:^(MDCButton* _Nonnull view) {
view.frame = CGRectMake(15, 30, 50, 50);
[view setBackgroundColor:[UIColor add_colorWithRGBHexString:@"#178EDA"] forState:UIControlStateNormal];
}];
when we need use category_button
:
MDCButton* clone = [MDCButton cloneForKey:@"category_button"];
Use copyTimes
function to layout views with same style :
- (void)copyTimes:(NSUInteger)times make:(void(^)(id view, NSUInteger idx))makeBlock;
for example:
UILabel* label = [[UILabel alloc] init];
[label copyTimes:10 make:^(id _Nonnull view, NSUInteger idx) {
[self.view addSubview:view];
[view setFrame:CGRectMake(0, idx * 100, 50, 100)];
}];