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:
- iOS 视图样式 该怎么样复用
- iOS最佳实践:视图生命周期该干什么
- iOS最佳实践:视图生命周期该干什么
- [IOS 视图布局]--分享样式REComposeViewController
- iOS文本框样式,键盘样式,清除按钮设置,左右视图
- 该怎么样用C++来实现类Unity3d的AddComponent
- 鼠标样式,想怎么样就怎么样
- iOS 开发 解决UICollectionView的多组头部视图样式不一样复用时发生错乱问题
- iOS开发之样式多样好用的滑动视图Demo
- 该怎么样学好这种语言??
- 何时该用DOM脚本设置样式
- iOS-设置视图圆角样式流畅解决方案
- 【iOS开发-15】UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等
- iOS开发UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等
- iOS 开发 UITextField常用属性:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等
- iOS开发中如何在View视图中获取到该视图的控制器
- webview网页视图用css控制样式
- 过去10天了。。。该怎么样呢?
- C:单链表的循环和递归实现
- 那些青葱岁月回忆录
- MySQL使用explain分析sql效率
- C语言的发展历史
- http://www.jianshu.com/p/94e0f9ab3f1d
- iOS 视图样式 该怎么样复用
- 100. Same Tree
- JavaScript中变量的类型
- MySQL使用存储过程批量插入百(千)万测试数据
- Java常用的八种排序算法与代码实现
- unity shader:渲染优化
- 浏览器判断是否启用了flash
- 机器学习笔记--神经网络--Rosenblatt
- 甲骨文公司在Sparc T4高端服务器上下了重注