XMHConvenientUIKit
介绍:XMHConvenientUIKit 是通过链式语法创建UIKit控件。写这个库就一个目的,开发中快速创建 UI 控件。依赖 Masonry。说下优缺点
优点:快速创建 UI, 可以一行形式写代码,也相比原有 API 代码更紧凑、简便。
缺点:我认为链式语法在执行时相比原生执行要多调用很多方法,和创建多个 Block 对象。当然对于现在的 CPU、RAM 来说,这是微乎其微的,如果在意这些请不要使用。
GitHub地址:https://github.com/kongfanwu/XMHConvenientUIKit
代码示例
self.la = UILabel
.xmhNewAndSuperView(self.view)
.xmhTextAndTextColorAndFont(@"234567", UIColor.redColor, [UIFont systemFontOfSize:14])
.xmhTextAlignment(NSTextAlignmentCenter)
.xmhBackgroundColor(UIColor.blueColor)
.xmhCornerRadius(10)
.xmhBorderWidth(2)
.xmhBorderColor(UIColor.cyanColor)
.xmhMakeConstraints(^(MASConstraintMaker * make){
make.size.mas_equalTo(CGSizeMake(200, 44));
make.centerX.equalTo(self.view);
make.top.mas_equalTo(100);
});
self.button = UIButton
.xmhNewAndSuperView(self.view)
.xmhSetTitleAndColorAndFontAndState(@"title", UIColor.redColor, [UIFont systemFontOfSize:30], UIControlStateNormal)
.xmhSetBackgroundImageAndState([UIImage imageNamed:@"1"], UIControlStateNormal)
.xmhMakeConstraints(^(MASConstraintMaker * make){
make.size.mas_equalTo(CGSizeMake(200, 80));
make.top.equalTo(self.la.mas_bottom);
make.centerX.equalTo(self.view);
})
.xmhAddEvent(UIControlEventTouchUpInside, ^(UIButton *sender){
NSLog(@"UIControlEventTouchUpInside");
});
self.imageView = UIImageView
.xmhNewAndSuperView(self.view)
.xmhImage([UIImage imageNamed:@"1"])
.xmhMakeConstraints(^(MASConstraintMaker * make){
make.size.mas_equalTo(CGSizeMake(100, 100));
make.top.equalTo(self.button.mas_bottom);
make.centerX.equalTo(self.view);
});
self.textField = UITextField
.xmhNewAndSuperView(self.view)
.xmhText(@"text")
.xmhPlaceholder(@"place")
.xmhDelegate(self)
.xmhBackgroundColor(UIColor.orangeColor)
.xmhMakeConstraints(^(MASConstraintMaker * make){
make.size.mas_equalTo(CGSizeMake(200, 50));
make.top.equalTo(self.imageView.mas_bottom);
make.centerX.equalTo(self.view);
});
self.tableView = UITableView
.xmhNewAndSuperViewAndFrameAndStyleAndDelegate(self.view, CGRectMake(0, 0, 0, 0), UITableViewStylePlain, self)
.xmhMakeConstraints(^(MASConstraintMaker * make){
make.edges.equalTo(self.view);
});
由于链式语法以Block形式调用、传参。开发中直接调用 Xcode 并不能直接显示方法全名,所以我写了一套代码片段。调用时请使用前缀 xmh... 形式。
代码片段地址
此框架的核心技术就是使用Category对类扩展方法,但是由于UIKit类比较多,每个类都创建Category重复性工作比较多,所以我写了一套宏可以很方便的对一个类扩展。
//
// UIView+XMHConvenient.h
// XMHUIKit
//
// Created by kfw on 2019/12/13.
// Copyright © 2019 神灯智能. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Masonry.h"
// 创建分类
#define XMHConvenientCreateCateory(ClassType, CategoryName, ProtocolName) \
@interface ClassType (CategoryName) <ProtocolName> \
@end \
@implementation ClassType (CategoryName) \
@end \
// 创建协议、分类,并让分类遵循协议。 ...动态参数为协议需要添加的方法
#define XMHConvenientCreateProtocolAndCategory(ClassType, ...) \
@protocol XMH##ClassType##Convenient2Protocol <NSObject> \
@optional \
XMHConvenientUIViewMethods(__kindof ClassType *, \
__VA_ARGS__ \
) \
@end \
XMHConvenientCreateCateory(ClassType, XMHConvenient2, XMH##ClassType##Convenient2Protocol) \
// 定义UIControl基类方法
#define XMHConvenientUIControlMethods(ClassType, ...) \
__VA_ARGS__ \
- (__kindof ClassType (^)(UIControlEvents controlEvents, void(^)(ClassType)))xmhAddEvent; \
- (__kindof ClassType(^)(BOOL))xmhEnabled; \
- (__kindof ClassType(^)(BOOL))xmhSelected; \
- (__kindof ClassType(^)(BOOL))xmhHighlighted; \
// 定义UIView基类方法
#define XMHConvenientUIViewMethods(ClassType, ...) \
__VA_ARGS__ \
+ (ClassType (^)(UIView *))xmhNewAndSuperView; \
- (ClassType(^)(UIView *))xmhSuperView; \
- (ClassType(^)(CGRect))xmhFrame; \
- (ClassType(^)(void(^)(MASConstraintMaker *)))xmhMakeConstraints; \
- (ClassType(^)(UIColor *))xmhBackgroundColor; \
- (ClassType(^)(CGFloat))xmhCornerRadius; \
- (ClassType(^)(CGFloat))xmhBorderWidth; \
- (ClassType(^)(UIColor *))xmhBorderColor; \
- (ClassType(^)(CGFloat, UIColor *))xmhBorderWidthAndColor; \
- (ClassType(^)(BOOL))xmhHidden; \
- (UIView *(^)(NSInteger))xmhTag; \
NS_ASSUME_NONNULL_BEGIN
// 可直接写成此宏,考虑此文件是UIVeiw和应该有示例代码。此处不替换。
//XMHConvenientCreateProtocolAndCategory(UIView,
//+ (instancetype)xmhNew;
//)
@protocol XMHUIViewConvenient2Protocol <NSObject>
@optional
XMHConvenientUIViewMethods(__kindof UIView *,
+ (instancetype)xmhNew;
)
@end
XMHConvenientCreateCateory(UIView, XMHConvenient2, XMHUIViewConvenient2Protocol)
XMHConvenientCreateProtocolAndCategory(UILabel)
XMHConvenientCreateProtocolAndCategory(UIImageView)
XMHConvenientCreateProtocolAndCategory(UITextField)
XMHConvenientCreateProtocolAndCategory(UITextView)
XMHConvenientCreateProtocolAndCategory(UIControl,
XMHConvenientUIControlMethods(UIControl *)
// 在此可添加协议方法定义。例如:
// - (void)test;
)
XMHConvenientCreateProtocolAndCategory(UIButton,
XMHConvenientUIControlMethods(UIButton *)
)
XMHConvenientCreateProtocolAndCategory(UITableView)
NS_ASSUME_NONNULL_END