Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I change the font color of the datePicker in iOS7?

just downloaded my copy of xcode 5 and was wondering if anyone knows how I can change the color or size of the font in the date picker?

like image 747
Jared Gross Avatar asked Sep 15 '13 01:09

Jared Gross


People also ask

How do I change the color of the date picker text?

Here's how you do it: Step 1: Inside the showDatePicker function, add the builder paramter. Step 2: Inside the builder parameter, return the Theme widget. Step 3: Inside the Theme widget, add the data property and define the new theme by specifying the colorScheme for the date picker dialog.

How do I change the color of my datePicker?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Kindly fine the highlighted code, this is the simplest way to change the colour of your datePicker.


2 Answers

I need similar for my app and have ended up going the long way round. It's a real shame there isn't an easier way to simply switch to a white text version of UIDatePicker.

The code below uses a category on UILabel to force the label's text colour to be white when the setTextColor: message is sent to the label. In order to not do this for every label in the app I've filtered it to only apply if it's a subview of a UIDatePicker class. Finally, some of the labels have their colours set before they are added to their superviews. To catch these the code overrides the willMoveToSuperview: method.

It would likely be better to split the below into more than one category but I've added it all here for ease of posting.

#import "UILabel+WhiteUIDatePickerLabels.h" #import <objc/runtime.h>  @implementation UILabel (WhiteUIDatePickerLabels)  + (void)load {     static dispatch_once_t onceToken;     dispatch_once(&onceToken, ^{         [self swizzleInstanceSelector:@selector(setTextColor:)                       withNewSelector:@selector(swizzledSetTextColor:)];         [self swizzleInstanceSelector:@selector(willMoveToSuperview:)                       withNewSelector:@selector(swizzledWillMoveToSuperview:)];     }); }  // Forces the text colour of the label to be white only for UIDatePicker and its components -(void) swizzledSetTextColor:(UIColor *)textColor {     if([self view:self hasSuperviewOfClass:[UIDatePicker class]] ||        [self view:self hasSuperviewOfClass:NSClassFromString(@"UIDatePickerWeekMonthDayView")] ||        [self view:self hasSuperviewOfClass:NSClassFromString(@"UIDatePickerContentView")]){         [self swizzledSetTextColor:[UIColor whiteColor]];     } else {         //Carry on with the default         [self swizzledSetTextColor:textColor];     } }  // Some of the UILabels haven't been added to a superview yet so listen for when they do. - (void) swizzledWillMoveToSuperview:(UIView *)newSuperview {     [self swizzledSetTextColor:self.textColor];     [self swizzledWillMoveToSuperview:newSuperview]; }  // -- helpers -- - (BOOL) view:(UIView *) view hasSuperviewOfClass:(Class) class {     if(view.superview){         if ([view.superview isKindOfClass:class]){             return true;         }         return [self view:view.superview hasSuperviewOfClass:class];     }     return false; }  + (void) swizzleInstanceSelector:(SEL)originalSelector                  withNewSelector:(SEL)newSelector {     Method originalMethod = class_getInstanceMethod(self, originalSelector);     Method newMethod = class_getInstanceMethod(self, newSelector);      BOOL methodAdded = class_addMethod([self class],                                        originalSelector,                                        method_getImplementation(newMethod),                                        method_getTypeEncoding(newMethod));      if (methodAdded) {         class_replaceMethod([self class],                             newSelector,                             method_getImplementation(originalMethod),                             method_getTypeEncoding(originalMethod));     } else {         method_exchangeImplementations(originalMethod, newMethod);     } }  @end 
like image 107
Sig Avatar answered Sep 19 '22 11:09

Sig


This is the simplest way and it works! I Promise. Create the UIDatePicker in the view, or connect it via a storyboard, then put the following code into the ViewDidLoad method. Replace "datepicker" in the first line with whatever you named your date picker.

Objective-C:

//Set Color of Date Picker self.datePicker.datePickerMode = UIDatePickerModeDate; [self.datePicker setValue:[UIColor colorWithRed:70/255.0f green:161/255.0f blue:174/255.0f alpha:1.0f] forKeyPath:@"textColor"]; SEL selector = NSSelectorFromString(@"setHighlightsToday:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]]; BOOL no = NO; [invocation setSelector:selector]; [invocation setArgument:&no atIndex:2]; [invocation invokeWithTarget:self.datePicker]; 
like image 33
Trianna Brannon Avatar answered Sep 19 '22 11:09

Trianna Brannon