一、前言
每次新開項目都需要寫一大篇同樣的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我。
- 聯系我?? :簡書 微博