You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by cx...@apache.org on 2017/04/11 13:40:57 UTC

[2/6] incubator-weex git commit: + [ios] picker support custom title title color background color and etc

+ [ios] picker support custom title title color background color and etc


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/151b3fdb
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/151b3fdb
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/151b3fdb

Branch: refs/heads/0.12-dev
Commit: 151b3fdbe5d0e479a5baa17e6d56987588af673a
Parents: c597a05
Author: \u9f50\u5c71 <su...@163.com>
Authored: Fri Apr 7 17:44:20 2017 +0800
Committer: \u9f50\u5c71 <su...@163.com>
Committed: Fri Apr 7 17:44:20 2017 +0800

----------------------------------------------------------------------
 ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m | 122 ++++++++++++++++++-
 1 file changed, 116 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/151b3fdb/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m
----------------------------------------------------------------------
diff --git a/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m b/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m
index a587d4c..c942464 100644
--- a/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m
+++ b/ios/sdk/WeexSDK/Sources/Module/WXPickerModule.m
@@ -23,6 +23,17 @@
 @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 @@ WX_EXPORT_METHOD(@selector(pickTime: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 @@ WX_EXPORT_METHOD(@selector(pickTime:callback:))
     [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 @@ WX_EXPORT_METHOD(@selector(pickTime:callback:))
 -(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 @@ WX_EXPORT_METHOD(@selector(pickTime:callback:))
 - (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