ios 更改 UIDatePicker 字体颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20875054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change UIDatePicker font color?
提问by Milo
All I want to do is change the font color of the UIDatePicker. I've researched other questions but they're all involving changing other properties and customizing the entire look. All I want to do is just change the font color from black to white. I find it hard to believe that I can't do such a seemingly simple task. And why wouldn't the tint color affect it? Does it even do anything?
我想要做的就是更改 UIDatePicker 的字体颜色。我研究了其他问题,但它们都涉及更改其他属性和自定义整个外观。我想要做的只是将字体颜色从黑色更改为白色。我很难相信我做不到这么简单的任务。为什么色调颜色不会影响它?它甚至做任何事情吗?
回答by waffles
All I need (on iOS 8.x and 9.0) is this one line to change the font color:
我只需要(在 iOS 8.x 和 9.0 上)这一行来更改字体颜色:
[my_date_picker setValue:[UIColor whiteColor] forKey:@"textColor"];
No subclassing or invoking of private APIs...
没有子类化或调用私有 API...
Note: today's current date will still be highlighted (in newer iOS versions). You can get around this by using the following code:
注意:今天的当前日期仍将突出显示(在较新的 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
}
回答by theDC
As of Swift 2.1:
从 Swift 2.1 开始:
picker.setValue(UIColor.whiteColor(), forKey: "textColor")
picker.sendAction("setHighlightsToday:", to: nil, forEvent: nil)
let date = NSDate()
picker.setDate(date, animated: false)
Instead of "today" you will see the current day,
而不是“今天”,你会看到今天,
回答by Toldy
One other alternative to @Jeremiah answeris to define those values in Interface Builder. I prefer this one because there is no code to add in your ViewController.
@Jeremiah 答案的另一种替代方法是在 Interface Builder 中定义这些值。我更喜欢这个,因为没有代码可以添加到您的 ViewController 中。
Click into your DatePicker view and customise it from the Attribute inspector as in the screenshot.
单击您的 DatePicker 视图并从属性检查器自定义它,如屏幕截图所示。
Works for Swift 2, 3, 4 and probably for Swift < 2. (not tested)
适用于 Swift 2、3、4,可能适用于 Swift < 2。(未测试)
回答by Jose Manuel Abarca Rodríguez
Next solution comes from "arturgrigor" and it works great in my apps, just copy it, paste it in viewDidLoad method, and enjoy it :
下一个解决方案来自“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];
回答by Michael Dautermann
According to Apple's UIKit User Interface Catalog
, developers are not allowed to customize date pickers.
根据Apple's UIKit User Interface Catalog
,不允许开发人员自定义日期选择器。
I've seen other StackOverflow answers for similar questions that suggest making a fake UIDatePicker using UIPickerView and customizing that.
我已经看到其他类似问题的 StackOverflow 答案,这些答案建议使用 UIPickerView 制作一个假的 UIDatePicker 并自定义它。
I also found an open source date picker on GitHub (at https://github.com/mwermuth/MWDatePicker )that might help a bit. It allows for different background and selector styles, but not a different font or font attributes.... yet.
我还在 GitHub 上找到了一个开源日期选择器(位于 https://github.com/mwermuth/MWDatePicker ),它可能会有所帮助。它允许不同的背景和选择器样式,但不允许不同的字体或字体属性......
回答by Daniel Sumara
To change UIDatePicker text color use:
要更改 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)
}
It works like a charm with xCode 7.3 and Swift 2.3.
它在 xCode 7.3 和 Swift 2.3 中就像一个魅力。
回答by Jeremiah
If anyone wants the swift solution, I placed the following in viewDidLoad:
如果有人想要快速解决方案,我在 viewDidLoad 中放置了以下内容:
birthdayDatePicker.setValue(DesignHelper.getOffWhiteColor(), forKey: "textColor")
birthdayDatePicker.performSelector("setHighlightsToday:", withObject:DesignHelper.getOffWhiteColor())
回答by BandoKal
You can also add this as an IBDesignable if you want to configure this within InterFace Builder.
如果您想在 InterFace Builder 中配置它,您也可以将其添加为 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.
}
}
}
回答by datinc
This subclass of UIDatePicker works for iOS 7. Its not pretty but gets the job done.
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
回答by Srj0x0
[date_picker setValue:textColor forKey:@"textColor"];
[date_picker performSelector:@selector(setHighlightsToday:) withObject:NO];