SGCreateViewTool
便捷创建 UIView 及其子类。详见 SGCreateViewTool.h
示例
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
安装
SGCreateViewTool 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
pod 'SGCreateViewTool'
作者
slagslag, [email protected]
说明
颜色可传 UIColor 或 @"0xF0F", @"66ccff", @"#66CCFF88",按钮图片可传 UIImage 或者 UIColor 或者 色值字符
- UIView 通用方法
// 初始化
UIView.add(CGRectMake(0, 0, 10, 10))
UIView.init
UIView.new
// 通用设置
.rect(CGRectMake(0, 0, 10, 10)) // 位置
.backColor([UIColor redColor]) // 背景颜色
.borderWidth(2) // 边框宽度
.borderColor([UIColor blackColor])// 边框颜色
.cornerRadius(5) // 圆角
.superView(self.view) // 添加到父视图
.targetSel(self, @selector(login))// 绑定事件
.clickedBlock = ^(UIView *view) {// 绑定事件回调
};
UILabel
// 初始化
UILabel.add(CGRectMake(0, 0, 10, 10))
UILabel.init
UILabel.new
.title(@"标题") // 文字
.titleColor([UIColor redColor]) // 文字颜色
.fontSize(15) // 字体大小
.UIFont([UIFont boloSystemFontOfSize:20])// 字体
.aligment(NSTextAlignmentCenter) // 对齐方式
.lines(0) // 行数
UIButton
// 初始化
UIButton.add(CGRectMake(0, 0, 10, 10))
UIButton.init
UIButton.new
.title(@"登录") //文字
.titleState(@"未登录", UIControlStateSelected)//文字及状态
.titleColor([UIColor blackColor])//文字颜色
.titleColorState([UIColor redColor], UIControlStateSelected)//文字颜色及状态
.fontSize(18)//文字大小
.UIFont([UIFont boloSystemFontOfSize:20])//字体
.image([UIImage imageNamed:@"aa"], UIControlStateNormal)//前景图片及状态
.backImage([UIImage imageNamed:@"back"], UIControlStateNormal)//背景图片及状态
.verticalAlignment(UIControlContentVerticalAlignmentTop)//垂直对齐方式
.horizontalAlignment(UIControlContentHorizontalAlignmentLeft)//水平对齐方式
.targetSelEvent(self, @selector(login), UIControlEventValueChanged)//事件绑定
UIImageView
// 初始化
UIImageView.add(CGRectMake(0, 0, 10, 10))
UIImageView.init
UIImageView.new
.sg_image([UIImage imageNamed:@"lyf"]) //图片
.sg_imageDefaultMode([UIImage imageNamed:@"lyf"]) //默认填充模式下设置图片
.mode(UIViewContentModeScaleAspectFill)//填充模式
UITableView
1. 初始化
// 初始化不注册cell
+ (instancetype)addTabeView:(CGRect)rect style:(UITableViewStyle)style delegate:(id)delegate backColor:(id)backColor superView:(UIView * _Nullable)superView;
// 初始化并注册cell
+ (instancetype)addTabeView:(CGRect)rect style:(UITableViewStyle)style delegate:(id)delegate backColor:(id)backColor superView:(UIView * _Nullable)superView cells:(NSString *)cells,...
2. 注册 cell
- (void)registerCells:(NSString *)cells,...;
// Example:
[self.tableView registerCells:@"SGTableViewCell",@"SGHomeTableViewCell",nil];
3. 注册 headerFooter
- (void)registerHeaderFooters:(NSString *)headerFooters,...;
// Example:
[self.tableView registerHeaderFooters:@"SGTableViewHeader",@"SGTableViewFooter",nil];
UICollectionView
- 初始化
// 初始化不注册 cell
+ (instancetype)addCollectionView:(CGRect)rect layout:(UICollectionViewLayout *)layout delegate:(id)delegate backColor:(id)backColor superView:(UIView * _Nullable)superView;
// 初始化并注册cell
+ (instancetype)addCollectionView:(CGRect)rect layout:(UICollectionViewLayout * _Nonnull)layout delegate:(id)delegate backColor:(id)backColor superView:(UIView * _Nullable)superView cells:(NSString *)cells,...;
2. 注册 cell
- (void)registerCells:(NSString *)cells,...;
// Example:
[self.collectionView registerCells:@"SGCollectionViewCell",@"SGHomeCollectionViewCell",nil];
3. 注册 header
- (void)registerHeaders:(NSString *)cells,...;
// Example:
[self.collectionView registerHeaders:@"SGCollectionViewHeader",@"SGHomeCollectionViewHeader",nil];
4. 注册 footer
- (void)registerFooters:(NSString *)cells,...;
// Example:
[self.collectionView registerFooters:@"SGCollectionViewFooter",@"SGHomeCollectionViewFooter",nil];
UITableViewCell 使用该方法不需要提前注册 cell
// 默认 cell 模式创建并复用 cell
+ (instancetype)dequeueReusableCellWithTableView:(UITableView * _Nonnull)tableView;
// 创建并复用 cell
+ (instancetype)dequeueReusableCellWithTableView:(UITableView * _Nonnull)tableView cellStyle:(UITableViewCellStyle)style;;
// Example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MineTableViewCell *cell = [MineTableViewCell dequeueReusableCellWithTableView:tableView];
return cell;
}
// Example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MineTableViewCell *cell = [MineTableViewCell dequeueReusableCellWithTableView:tableView cellStyle:(UITableViewCellStyleSubtitle)];
return cell;
}