更改UIDatePicker字体颜色?

我想要做的就是改变UIDatePicker的字体颜色。 我研究了其他问题,但都涉及到更改其他属性并自定义整个外观。 我想要做的只是将字体颜色从黑色更改为白色。 我觉得很难相信我不能做这样一个看似简单的任务。 为什么色彩不会影响它? 它甚至做什么?

所有我需要的(在iOS 8.x和9.0)是这一行改变字体颜色:

[my_date_picker setValue:[UIColor whiteColor] forKey:@"textColor"]; 

没有子类或调用私有API …


注意:今天的当前date仍将突出显示(在较新的iOS版本中)。 你可以通过使用下面的代码来解决这个问题:

 if ([my_date_picker respondsToSelector:sel_registerName("setHighlightsToday:")]) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wundeclared-selector" [my_date_picker performSelector:@selector(setHighlightsToday:) withObject:[NSNumber numberWithBool:NO]]; #pragma clang diagnostic pop } 

下一个解决scheme来自“arturgrigor”,它在我的应用程序中效果很好,只需复制它,粘贴到viewDidLoad方法中,并享受它:

 [my_datePicker setValue:[UIColor whiteColor] 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:my_datePicker]; 

从Swift 2.1开始:

 picker.setValue(UIColor.whiteColor(), forKey: "textColor") picker.sendAction("setHighlightsToday:", to: nil, forEvent: nil) let date = NSDate() picker.setDate(date, animated: false) 

而不是“今天”你会看到今天,

@Jeremiah答案的另一种替代方法是在Interface Builder中定义这些值。 我更喜欢这个,因为没有代码添加到您的ViewController中。

点击进入DatePicker视图,并在屏幕截图中的属性检查器中进行自定义。 截图

适用于Swift 2,可能适用于Swift <2.(未testing)

根据苹果的UIKit用户界面目录 ,开发者不允许自定义dateselect器。

我见过其他的StackOverflow的答案类似的问题, build议使用UIPickerView和自定义的假UIDatePicker 。

我还在GitHub(https://github.com/mwermuth/MWDatePicker)上find了一个开源的dateselect&#x5668; ,可能会有所帮助。 它允许不同的背景和select器样式,但不是一个不同的字体或字体属性….还没有。

如果有人想要迅速的解决scheme,我把下面的viewDidLoad:

  birthdayDatePicker.setValue(DesignHelper.getOffWhiteColor(), forKey: "textColor") birthdayDatePicker.performSelector("setHighlightsToday:", withObject:DesignHelper.getOffWhiteColor()) 

要更改UIDatePicker文本颜色,请使用:

  // MARK: - Helping content private enum DatePickerProperties: String { case TextColor = "textColor" case HighlightsToday = "highlightsToday" } // MARK: - Outlets @IBOutlet private weak var datePicker: UIDatePicker! // MARK: - Lifecicle public override func awakeFromNib() { super.awakeFromNib() self.datePicker.setValue(UIColor.whiteColor(), forKey: DatePickerProperties.TextColor.rawValue) self.datePicker.setValue(false, forKey: DatePickerProperties.HighlightsToday.rawValue) } 

它像xCode 7.3和Swift 2.3一样的魅力。

这个UIDatePicker的子类适用于iOS 7.它不漂亮,但完成了工作。

 #define kNotification_UIView_didAddSubview @"kNotification_UIView_didAddSubview" @implementation UIView (addSubview) -(void) didAddSubview:(UIView *)subview{ [[NSNotificationCenter defaultCenter] postNotificationName:kNotification_UIView_didAddSubview object:self]; } @end @interface DatePicker () @property (nonatomic, strong) UIColor* textColor; @end @implementation DatePicker -(id) initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self){ [self setup]; } return self; } -(id) initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if (self){ [self setup]; } return self; } -(void) setup{ self.textColor = [UIColor darkTextColor]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subviewsUpdated:) name:kNotification_UIView_didAddSubview object:nil]; } -(void) dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } -(void) updateLabels:(UIView*) view{ for (UILabel* label in view.subviews){ if ([label isKindOfClass:[UILabel class]]){ label.textColor = self.textColor; }else{ [self updateLabels:label]; } } } -(BOOL) isSubview:(UIView*) view{ if (view == nil){ return NO; } if (view.superview == self){ return YES; } return [self isSubview:view.superview]; } -(void) subviewsUpdated:(NSNotification*) notification{ if ([notification.object isKindOfClass:NSClassFromString(@"UIPickerTableView")] && [self isSubview:notification.object]){ [self updateLabels:notification.object]; } } @end 

如果要在InterFace Builder中对其进行configuration,也可以将其添加为IBDesignable。

  import UIKit @IBDesignable extension UIDatePicker { @IBInspectable var textLabelColor: UIColor? { get { return self.valueForKey("textColor") as? UIColor } set { self.setValue(newValue, forKey: "textColor") self.performSelector("setHighlightsToday:", withObject:newValue) //For some reason this line makes the highlighted text appear the same color but can not be changed from textColor. } } } 

作为@Jeremiah答案的替代,你可以使用这个:

 datePicker.setValue(UIColor.redColor(), forKey: "textColor") datePicker.sendAction("setHighlightsToday:", to: nil, forEvent: nil) datePicker.setDate(NSDate(timeIntervalSinceReferenceDate: 0), animated: false) 

它会删除今天(你会看到当前的date),但它将与正确的颜色。

可能的麻烦:如果你dynamic改变颜色,我没有find一个方法来重新加载dateselect器。 因此,用户将看到以前的颜色,只有滚动后,颜色才会变成新的。 – >解决,由最后一个string。 看起来像巫毒,但它的工作原理…

这个答案适合Swift 2.1

 [date_picker setValue:textColor forKey:@"textColor"]; [date_picker performSelector:@selector(setHighlightsToday:) withObject:NO]; 

从Storyboard中添加名为“textColor”的运行时属性,如下图所示。

在这里输入图像说明

对于Xamarin开发人员:

 DatePicker.SetValueForKey(UIColor.White, new NSString("textColor")); DatePicker.SetValueForKey(FromObject(false), new NSString("highlightsToday")); 

它像一个魅力工作。 在iOS 9和10testing

我偶然发现了使用UIAppearance一个令人惊讶的干净的解决scheme,不使用任何KVC, UIAppearance或其他私有的API。 我发现试图通过UIAppearanceUIDatePicker任何UILabel设置textColor没有任何影响,但一个自定义外观属性,简单地称为常规textColor设置工作得很好。

 // Implement a custom appearance property via a UILabel category @interface UILabel (PickerLabelTextColor) @property (nonatomic, strong) UIColor * textColorWorkaround UI_APPEARANCE_SELECTOR; @end @implementation UILabel (PickerLabelTextColor) - (UIColor *)textColorWorkaround { return self.textColor; } - (void)setTextColorWorkaround:(UIColor *)textColor { self.textColor = textColor; } @end 

然后使用如下:

 UILabel *pickerLabelProxy = [UILabel appearanceWhenContainedInInstancesOfClasses:@[UIDatePicker.class]]; pickerLabelProxy.textColorWorkaround = UIColor.lightGrayColor;