简体   繁体   English

UIDatePicker 更改“今天”日期文本的颜色

[英]UIDatePicker Change color of text of "today" date

I know that this question as been made other times, but with xCode 7 and iOS9.2 is it possible to change the font color of "today" date on a UIDatePicker?我知道这个问题是在其他时间提出的,但是使用 xCode 7 和 iOS9.2 是否可以更改 UIDatePicker 上“今天”日期的字体颜色?

[self.dpData setValue:[UIColor colorWithRed:111/255.0f green:113/255.0f blue:121/255.0f alpha:1.0f] forKeyPath:@"setHighlightsToday"];

I tried this and it crashed, so I know that this don't work.我试过了,但它崩溃了,所以我知道这行不通。

Any other options that I have, or I really need to build my own DatePicker?我还有其他选择吗,或者我真的需要构建自己的 DatePicker?

Thanks in advance.提前致谢。

(It has been reported that this approach crashes in iOS 14 so it is no longer valid. Please see the discussion below.) (据报道,这种方法在 iOS 14 中崩溃,因此不再有效。请参阅下面的讨论。)

A safer/easier way to do it is to simply set the value of the key "highlightsToday".一种更安全/更简单的方法是简单地设置键“highlightsToday”的值。 Here's a Swift 2.2 example:这是一个 Swift 2.2 示例:

datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")

I used the same method Richardo until now, but it is using a private api that could always become unavailable or Apple might reject it.到目前为止,我使用相同的方法 Richardo,但它使用的私有 api 可能总是不可用或 Apple 可能会拒绝它。 I just changed my code (as hacky and ugly as it is) to just change the date picker to a different mode and back - this causes the Today color to get the textColor that was set before.我只是更改了我的代码(尽管如此笨拙和丑陋),只需将日期选择器更改为不同的模式并返回 - 这会导致 Today 颜色获得之前设置的 textColor 。

selectedColor = [UIColor whiteColor];
[self.TimerPicker setValue:selectedColor forKeyPath:@"textColor"];
[self.TimerPicker setDatePickerMode:UIDatePickerModeDate];
[self.TimerPicker setDatePickerMode:UIDatePickerModeDateAndTime];

good enough for me.对我来说足够好了。

With some more search I found the solution.通过更多的搜索,我找到了解决方案。

[_dpData setValue:[UIColor colorWithRed:111/255.0f green:113/255.0f blue:121/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:_dpData];

With this solution we can change the color of the text (even today date) without problems.使用此解决方案,我们可以毫无问题地更改文本的颜色(即使是今天的日期)。

Actually given answers are still actual even for iOS 13.4 or 14. The reason of the crash is that you can set these parameters only for date picker with classic (Wheels) style:实际上,即使对于 iOS 13.4 或 14,给出的答案仍然是实际的。崩溃的原因是您只能为经典(轮子)样式的日期选择器设置这些参数:

datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")

By default date picker has Automatic style:默认日期选择器具有自动样式: 在此处输入图片说明

So if device has iOS 13.4 or higher installed, it will use Compact style.因此,如果设备安装了 iOS 13.4 或更高版本,它将使用Compact样式。 But older iOS versions will use Wheels style.但较旧的 iOS 版本将使用 Wheels 风格。

So to solve the issue with iOS 13.4 or higher you should add a condition based on UIDatePickerStyle (it's available starting from 13.4).因此,要解决 iOS 13.4 或更高版本的问题,您应该添加基于 UIDatePickerStyle 的条件(从 13.4 开始可用)。 Like this:像这样:

if #available(iOS 13.4, *) {
            let style = self.startDatePicker.datePickerStyle
            if style == .wheels {
                self.startDatePicker.setValue(UIColor.red, forKey: "textColor")
                self.startDatePicker.setValue(false, forKey: "highlightsToday")
            }
        } else {
            self.startDatePicker.setValue(UIColor.red, forKey: "textColor")
            self.startDatePicker.setValue(false, forKey: "highlightsToday")
        }

Another way is to use classic date picker style - Wheels by setting it's style explicitly in a StoryBoard builder:另一种方法是通过在 StoryBoard 构建器中明确设置它的样式来使用经典的日期选择器样式 - Wheels:

在此处输入图片说明

Going through the documentation for this class and it's parent class it says it's still not possible浏览这个类的文档,它是父类,它说它仍然不可能

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDatePicker_Class/index.html#//apple_ref/doc/uid/TP40006806 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDatePicker_Class/index.html#//apple_ref/doc/uid/TP40006806

Yes you should make your own, since with an item picker it's possible.是的,您应该自己制作,因为可以使用项目选择器。

This is really stupid on Apple's behalf这代表苹果真的很愚蠢

Not possible, however you can force the dark mode on UIDatePicker:不可能,但是您可以在 UIDatePicker 上强制使用暗模式:

if #available(iOS 13.0, *) {
    UIApplication.shared.keyWindow?.overrideUserInterfaceStyle.dark
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM