一行代码集成UIPickerView,界面完全自定义

一、前言

每次新开项目都需要写一大篇同样的UIPickerView代码,而且样式不一样,挺浪费时间的,所以就自己封装了一个目前常见样式的ZJPickerView,一行代码即可集成,使用属性字典来添加想要控制的属性,界面控件完全支持自定义配置。

+ (void)zj_showWithDataList:(nonnull NSArray *)dataList
               propertyDict:(nullable NSDictionary *)propertyDict
                 completion:(nullable void(^)(NSString * _Nullable selectContent))completion;

废话不多说了,先上效果图:


Demo.gif

二、能做什么

  • 一行代码即可集成。
  • 支持任意列数据联动展示。
  • 支持自定义界面中子控件的文字颜色文字字体
  • 支持自定义PickerView的属性:PickerView的高度、PickerView一行的高度、选中内容和未选中内容文字属性
  • 支持自定义背景透明度和是否接收背景触摸事件。
  • 支持选择完内容回调,每一列选中的值用逗号分隔开。
  • 支持已选择内容是否展示在标题上及展示时是否默认滚动到已选择内容那一行。

三、安装

方式一:使用CocoaPods

pod 'ZJPickerView'

方式二:手动导入

  • ZJPickerView文件夹中的所有源代码拽入项目中
  • 导入主头文件:#import "ZJPickerView.h"
ZJPickerView.h                      
ZJPickerView.m

四、示例

方式一:直接使用

[ZJPickerView zj_showWithDataList:@[@"IT", @"销售", @"自媒体", @"游戏主播", @"产品策划"] propertyDict:nil completion:^(NSString *selectContent) {
    NSLog(@"ZJPickerView log tip:---> selectContent:%@", selectContent);
}];

方式二:自定义想要的PickerView

// 支持的属性列表
// content: NSString type
extern NSString * _Nonnull const ZJPickerViewPropertyCanceBtnTitleKey; // cance button Title(取消按钮)
extern NSString * _Nonnull const ZJPickerViewPropertySureBtnTitleKey;  // sure button Title(确定按钮)
extern NSString * _Nonnull const ZJPickerViewPropertyTipLabelTextKey;  // tipLabel text(选择提示标签)

// color: UIColor type
extern NSString * _Nonnull const ZJPickerViewPropertyCanceBtnTitleColorKey; // cance button Title color(取消按钮文字颜色)
extern NSString * _Nonnull const ZJPickerViewPropertySureBtnTitleColorKey;  // sure button Title color(确定按钮文字颜色)
extern NSString * _Nonnull const ZJPickerViewPropertyTipLabelTextColorKey;  // tipLabel text color(选择提示标签文字颜色)
extern NSString * _Nonnull const ZJPickerViewPropertyLineViewBackgroundColorKey;  // lineView backgroundColor(分割线背景颜色)

// font: UIFont type
extern NSString * _Nonnull const ZJPickerViewPropertyCanceBtnTitleFontKey; // cance button label font, default 17.0f(取消按钮字体大小)
extern NSString * _Nonnull const ZJPickerViewPropertySureBtnTitleFontKey;  // sure button label font, default 17.0f(确定按钮字体大小)
extern NSString * _Nonnull const ZJPickerViewPropertyTipLabelTextFontKey;  // tipLabel font, default 17.0f(选择提示标题字体大小)

// pickerView:
// CGFloat type
extern NSString * _Nonnull const ZJPickerViewPropertyPickerViewHeightKey;  // pickerView height, default 224 pt(pickerView高度)
extern NSString * _Nonnull const ZJPickerViewPropertyOneComponentRowHeightKey;  // one component row height, default 32 pt(pickerView一行的高度)
// NSDictionary
extern NSString * _Nonnull const ZJPickerViewPropertySelectRowTitleAttrKey;  // select row titlt attribute(pickerView当前选中的文字颜色)
extern NSString * _Nonnull const ZJPickerViewPropertyUnSelectRowTitleAttrKey;  // unSelect row titlt attribute(pickerView当前没有选中的文字颜色)

// other: BOOL type
extern NSString * _Nonnull const ZJPickerViewPropertyIsTouchBackgroundHideKey;  // touch background is hide, default NO(是否点击背景隐藏)
extern NSString * _Nonnull const ZJPickerViewPropertyIsShowTipLabelKey;  // is show tipLabel, default NO. note: if the value of this key`ZJPickerViewPropertyIsShowSelectContentKey` is YES, the value of ZJPickerViewPropertyIsShowTipLabelKey is ignored.(是否显示提示标签。注意,如果这个key`ZJPickerViewPropertyIsShowSelectContentKey`的值为YES,忽略ZJPickerViewPropertyIsShowTipLabelKey的值)
extern NSString * _Nonnull const ZJPickerViewPropertyIsShowSelectContentKey;  // scroll component is update and show select content in tipLabel, default NO(选择内容后是否更新选择提示标签)
extern NSString * _Nonnull const ZJPickerViewPropertyIsScrollToSelectedRowKey;  // when pickerView will show scroll to selected row, default NO. note:`ZJPickerViewPropertyTipLabelTextKey` Must pass by value(将要显示时是否滚动到已选择内容那一行,注意,选择提示标签tipLabel必须传内容,比如之前选择了`北京`,此时就需要传入`北京`)
extern NSString * _Nonnull const ZJPickerViewPropertyIsAnimationShowKey;  // show pickerView is need Animation, default YES(显示pickerView时是否带动画效果)
// CGFloat type
extern NSString * _Nonnull const ZJPickerViewPropertyBackgroundAlphaKey;  // background alpha, default 0.5(0.0~1.0)(背景视图透明度)
// 使用
// 1.Custom propery(自定义属性)
NSDictionary *propertyDict = @{ZJPickerViewPropertyCanceBtnTitleKey : @"取消",
                               ZJPickerViewPropertySureBtnTitleKey  : @"确定",
                               ZJPickerViewPropertyTipLabelTextKey  : [_selectContentLabel.text substringFromIndex:5], // @"提示内容"
                               ZJPickerViewPropertyCanceBtnTitleColorKey : [UIColor zj_colorWithHexString:@"#A9A9A9"],
                               ZJPickerViewPropertySureBtnTitleColorKey : [UIColor zj_colorWithHexString:@"#FF6347"],
                               ZJPickerViewPropertyTipLabelTextColorKey : [UIColor zj_colorWithHexString:@"#231F20"],
                               ZJPickerViewPropertyLineViewBackgroundColorKey : [UIColor zj_colorWithHexString:@"#dedede"],
                               ZJPickerViewPropertyCanceBtnTitleFontKey : [UIFont systemFontOfSize:17.0f],
                               ZJPickerViewPropertySureBtnTitleFontKey : [UIFont systemFontOfSize:17.0f],
                               ZJPickerViewPropertyTipLabelTextFontKey : [UIFont systemFontOfSize:17.0f],
                               ZJPickerViewPropertyPickerViewHeightKey : @300.0f,
                               ZJPickerViewPropertyOneComponentRowHeightKey : @40.0f,
                               ZJPickerViewPropertySelectRowTitleAttrKey : @{NSForegroundColorAttributeName : [UIColor zj_colorWithHexString:@"#FF6347"], NSFontAttributeName : [UIFont systemFontOfSize:15.0f]},
                               ZJPickerViewPropertyUnSelectRowTitleAttrKey : @{NSForegroundColorAttributeName : [UIColor zj_colorWithHexString:@"#A9A9A9"], NSFontAttributeName : [UIFont systemFontOfSize:15.0f]},
                               ZJPickerViewPropertyIsTouchBackgroundHideKey : @YES,
                               ZJPickerViewPropertyIsShowTipLabelKey : @YES,
                               ZJPickerViewPropertyIsShowSelectContentKey : @YES,
                               ZJPickerViewPropertyIsScrollToSelectedRowKey: @YES,
                               ZJPickerViewPropertyIsAnimationShowKey : @YES};

// 2.Show(显示)
__weak typeof(_selectContentLabel) weak_selectContentLabel = _selectContentLabel;
[ZJPickerView zj_showWithDataList:dataList propertyDict:propertyDict completion:^(NSString *selectContent) {
    NSLog(@"ZJPickerView log tip:---> selectContent:%@", selectContent);
}];

五、结语

  • 如果在使用过程中遇到BUG,请Issues我,谢谢。
  • 如果你想为ZJPickerView输出代码,请拼命Pull Requests我。
  • 联系我😯 :简书 微博
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,475评论 4 372
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,744评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,101评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,732评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,141评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,049评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,188评论 2 320
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,965评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,716评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,867评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,341评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,663评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,376评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,200评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,990评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,179评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,979评论 2 279