From 151b3fdbe5d0e479a5baa17e6d56987588af673a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BD=90=E5=B1=B1?= Date: Fri, 7 Apr 2017 17:44:20 +0800 Subject: [PATCH 1/3] + [ios] picker support custom title title color background color and etc --- .../WeexSDK/Sources/Module/WXPickerModule.m | 122 +++++++++++++++++- 1 file changed, 116 insertions(+), 6 deletions(-) diff --git a/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m b/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m index a587d4cedf..c94246471a 100644 --- a/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m +++ b/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m @@ -23,6 +23,17 @@ @interface WXPickerModule() @property(nonatomic,strong)UIView *backgroundView; @property(nonatomic,strong)UIView *pickerView; +//custom +@property(nonatomic,copy)NSString *title; +@property(nonatomic,strong)UIColor *titleColor; +@property(nonatomic,copy)NSString *cancelTitle; +@property(nonatomic,copy)NSString *confirmTitle; +@property(nonatomic,strong)UIColor *cancelTitleColor; +@property(nonatomic,strong)UIColor *confirmTitleColor; +@property(nonatomic,strong)UIColor *titleBackgroundColor; +@property(nonatomic)NSInteger height; +@property(nonatomic,strong)UIColor *textColor; +@property(nonatomic,strong)UIColor *selectionColor; //data @property(nonatomic,copy)NSArray *items; @property(nonatomic)BOOL isAnimating; @@ -54,6 +65,36 @@ -(void)pick:(NSDictionary *)options callback:(WXModuleCallback)callback if (options[@"index"]) { index = [WXConvert NSInteger:options[@"index"]]; } + if (options[@"title"]) { + self.title = [WXConvert NSString:options[@"title"]]; + } + if (options[@"titleColor"]) { + self.titleColor = [WXConvert UIColor:options[@"titleColor"]]; + } + if (options[@"cancelTitle"]) { + self.cancelTitle = [WXConvert NSString:options[@"cancelTitle"]]; + } + if (options[@"confirmTitle"]) { + self.confirmTitle = [WXConvert NSString:options[@"confirmTitle"]]; + } + if (options[@"cancelTitleColor"]) { + self.cancelTitleColor = [WXConvert UIColor:options[@"cancelTitleColor"]]; + } + if (options[@"confirmTitleColor"]) { + self.confirmTitleColor = [WXConvert UIColor:options[@"confirmTitleColor"]]; + } + if (options[@"titleBackgroundColor"]) { + self.titleBackgroundColor = [WXConvert UIColor:options[@"titleBackgroundColor"]]; + } + if (options[@"textColor"]) { + self.textColor = [WXConvert UIColor:options[@"textColor"]]; + } + if (options[@"selectionColor"]) { + self.selectionColor = [WXConvert UIColor:options[@"selectionColor"]]; + } + if (options[@"height"]) { + self.height = [WXConvert CGFloat:options[@"height"]]; + } if (items && [items count]>0 && [self isRightItems:items]) { [self createPicker:items index:index]; self.callback = callback; @@ -160,17 +201,52 @@ -(void)configPickerView [self.backgroundView addGestureRecognizer:tapGesture]; self.pickerView = [self createPickerView]; UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, WXPickerToolBarHeight)]; - [toolBar setBackgroundColor:[UIColor whiteColor]]; + toolBar.barTintColor = self.titleBackgroundColor?self.titleBackgroundColor:[UIColor whiteColor]; + + + UIBarButtonItem* noSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; noSpace.width=10; - UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)]; - UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; - UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)]; + + UIBarButtonItem* doneBtn ; + if (self.confirmTitle.length >0) { + doneBtn = [[UIBarButtonItem alloc] initWithTitle:self.confirmTitle style:UIBarButtonItemStyleBordered target:self action:@selector(done:)]; + }else { + doneBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)]; + } + if(self.confirmTitleColor){ + doneBtn.tintColor = self.confirmTitleColor; + } + UIBarButtonItem *cancelBtn; + if (self.cancelTitle.length >0) { + cancelBtn = [[UIBarButtonItem alloc] initWithTitle:self.cancelTitle style:UIBarButtonItemStyleBordered target:self action:@selector(cancel:)]; + }else { + cancelBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)]; + } + if(self.cancelTitleColor){ + cancelBtn.tintColor = self.cancelTitleColor; + } + UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [toolBar setItems:[NSArray arrayWithObjects:noSpace,cancelBtn,flexSpace,doneBtn,noSpace, nil]]; + UILabel *titleLabel = [UILabel new]; + titleLabel.frame = CGRectMake(0, 0, 200, WXPickerToolBarHeight); + titleLabel.center = toolBar.center; + titleLabel.textAlignment = NSTextAlignmentCenter; + if(self.titleColor){ + titleLabel.textColor = self.textColor; + } + if(self.title.length>0){ + titleLabel.text = self.title; + [toolBar addSubview:titleLabel]; + } [self.pickerView addSubview:toolBar]; self.picker = [[UIPickerView alloc]init]; self.picker.delegate = self; - CGRect pickerFrame = CGRectMake(0, WXPickerToolBarHeight, [UIScreen mainScreen].bounds.size.width, WXPickerHeight-WXPickerToolBarHeight); + CGFloat height = WXPickerHeight; + if (WXFloatEqual(self.height, 0)){ + height = self.height>WXPickerToolBarHeight?self.height:WXPickerHeight; + } + CGRect pickerFrame = CGRectMake(0, WXPickerToolBarHeight, [UIScreen mainScreen].bounds.size.width, height-WXPickerToolBarHeight); self.picker.backgroundColor = [UIColor whiteColor]; self.picker.frame = pickerFrame; [self.pickerView addSubview:self.picker]; @@ -180,7 +256,11 @@ -(void)configPickerView -(UIView *)createPickerView { UIView *view = [UIView new]; - view.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, WXPickerHeight); + CGFloat height = WXPickerHeight; + if (WXFloatEqual(self.height, 0)){ + height = self.height>WXPickerToolBarHeight?self.height:WXPickerHeight; + } + view.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, height); view.backgroundColor = [UIColor whiteColor]; return view; } @@ -213,8 +293,38 @@ - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row f - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { self.index = row; + if(self.selectionColor) { + UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component]; + [labelSelected setBackgroundColor:self.selectionColor?self.selectionColor:[UIColor whiteColor]]; + } +} + +- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{ + NSString * reStr = self.items[row]; + NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:reStr]; + UIColor *color = self.textColor?self.textColor:[UIColor blackColor]; + [attriStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, reStr.length)]; + + return attriStr; } +-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view +{ + + UILabel *label = (id)view; + + if (!label) + { + + label= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)]; + label.textAlignment = NSTextAlignmentCenter; + label.text = self.items[row]; + } + + return label; +} + + #pragma mark - #pragma Date & Time Picker -(void)pickDate:(NSDictionary *)options callback:(WXModuleCallback)callback From 1a4c9079b804f3bb495423926f9b16fac397e589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BD=90=E5=B1=B1?= Date: Fri, 7 Apr 2017 17:47:46 +0800 Subject: [PATCH 2/3] + [ios] update height type --- ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m b/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m index c94246471a..fb29d5589b 100644 --- a/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m +++ b/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m @@ -31,7 +31,7 @@ @interface WXPickerModule() @property(nonatomic,strong)UIColor *cancelTitleColor; @property(nonatomic,strong)UIColor *confirmTitleColor; @property(nonatomic,strong)UIColor *titleBackgroundColor; -@property(nonatomic)NSInteger height; +@property(nonatomic)CGFloat height; @property(nonatomic,strong)UIColor *textColor; @property(nonatomic,strong)UIColor *selectionColor; //data From cddebfb70fbe149bdfb6d24a68548a280bd3ca95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BD=90=E5=B1=B1?= Date: Mon, 10 Apr 2017 13:33:48 +0800 Subject: [PATCH 3/3] + [ios] delete no use code --- ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m b/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m index fb29d5589b..2933115878 100644 --- a/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m +++ b/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m @@ -295,7 +295,7 @@ - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComp self.index = row; if(self.selectionColor) { UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component]; - [labelSelected setBackgroundColor:self.selectionColor?self.selectionColor:[UIColor whiteColor]]; + [labelSelected setBackgroundColor:self.selectionColor]; } }