ios 在 iOS8/Swift 中为 UIDatePicker 设置文本颜色和字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28417217/
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
Set text color and font for UIDatePicker in iOS8/Swift
提问by Fenda
I've been having trouble trying to set the UIDatePicker
fontand color. Everything else in my app was fairly straightforward to adjust except this. Does anybody know how to do this? I'm using Swiftfor iOS8.
我在尝试设置UIDatePicker
font和color 时遇到了麻烦。除了这个之外,我的应用程序中的其他所有内容都非常容易调整。有人知道怎么做这个吗?我正在为iOS8使用Swift。
回答by Dylan Reich
Changing the date mode to something else seems to force a re-draw with the newly set text color.
将日期模式更改为其他内容似乎会强制使用新设置的文本颜色重新绘制。
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was
回答by Pravin Kamble
you just need to set 2 lines of code in viewdidLoad
/ viewWillAppear
accoding where you using DatePicker.
您只需要在viewdidLoad
/ viewWillAppear
accoding 中使用 DatePicker设置 2 行代码。
dobDatePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
dobDatePicker.setValue(false, forKey: "highlightsToday")
See the Result like this:
看到这样的结果:
回答by ObjectiveTC
I believe this is the definitive solution for countdown timers.
It's an expansion of yildirimosman's answer.
我相信这是倒数计时器的最终解决方案。
这是 yildirimosman 答案的扩展。
//text color
datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
//picker background
datePicker.subviews[0].subviews[0].backgroundColor = UIColor.clearColor() //the picker's own background view
//dividers
datePicker.subviews[0].subviews[1].backgroundColor = UIColor.whiteColor()
datePicker.subviews[0].subviews[2].backgroundColor = UIColor.whiteColor()
//labels: "hours" and "min"
datePicker.subviews[0].subviews[3].setValue(UIColor.lightGrayColor(), forKey: "textColor")
datePicker.subviews[0].subviews[4].setValue(UIColor.lightGrayColor(), forKey: "textColor")
//refresh the tableview (to force initial row textColor to change to white)
datePicker.subviews[0].setNeedsLayout()
datePicker.subviews[0].layoutIfNeeded()
回答by yildirimosman
you can use
您可以使用
datePicker.setValue(UIColor.whiteColor(), forKey: "textColor")
datePicker.setValue(false, forKey: "highlightsToday")
//for selector color
datePickerView.subviews[0].subviews[1].backgroundColor = UIColor.whiteColor()
datePickerView.subviews[0].subviews[2].backgroundColor = UIColor.whiteColor()
回答by Konstantin Kryzhanovsky
try this:
尝试这个:
/* set color for UIDatePicker font */
//text color of today string
self.datePicker.performSelector("setHighlightsToday:", withObject:Constants.Colors.mainHeaderColor)
//text color for hoglighted color
self.datePicker.performSelector("_setHighlightColor:", withObject:Constants.Colors.mainHeaderColor)
//other text color
self.datePicker.setValue(Constants.Colors.mainHeaderColor, forKey: "textColor")
回答by CedricSoubrie
回答by IvanPavliuk
Swift 4
斯威夫特 4
override func layoutSubviews() {
super.layoutSubviews()
datePickerView.setValue(UIColor.white, forKeyPath: "textColor")
}
回答by Aliunco
The only way for changing the font of UIDatePickerView (until now) is swizzling:
更改 UIDatePickerView 字体的唯一方法(直到现在)是 swizzling:
you can change the font by an extension of UILabel! (this is not recommended but it works!)
你可以通过 UILabel 的扩展来改变字体!(这不是推荐的,但它有效!)
import Foundation
import UIKit
public extension UILabel {
@objc func setFontSwizzled(font: UIFont) {
if self.shouldOverride() {
self.setFontSwizzled(font: UIFont.fontWith(style: .regular, size: 14))
} else {
self.setFontSwizzled(font: font)
}
}
private func shouldOverride() -> Bool {
let classes = ["UIDatePicker", "UIDatePickerWeekMonthDayView", "UIDatePickerContentView"]
var view = self.superview
while view != nil {
let className = NSStringFromClass(type(of: view!))
if classes.contains(className) {
return true
}
view = view!.superview
}
return false
}
private static let swizzledSetFontImplementation: Void = {
let instance: UILabel = UILabel()
let aClass: AnyClass! = object_getClass(instance)
let originalMethod = class_getInstanceMethod(aClass, #selector(setter: font))
let swizzledMethod = class_getInstanceMethod(aClass, #selector(setFontSwizzled))
if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
// switch implementation..
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}()
static func swizzleSetFont() {
_ = self.swizzledSetFontImplementation
}
}
and for changing the color you just simply call the function below:
要更改颜色,您只需调用以下函数:
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
if it's necessary to be re-rendered you need to call:
如果需要重新渲染,您需要调用:
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was
回答by lborgav
You can set value using forKeyPath: "textColor". The code:
您可以使用 forKeyPath: "textColor" 设置值。编码:
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
where datePicker is your UIDatePicker object, and the first parameter is the color that you want
其中 datePicker 是您的 UIDatePicker 对象,第一个参数是您想要的颜色
回答by Alsop
I wanted to do this but set it for when someone has the date set prior to now, or after now. I had to reload the data, but when I did it ended up setting it to the current DateTime when using the above example.
我想这样做,但将其设置为某人在现在之前或之后设置了日期。我不得不重新加载数据,但是当我使用上面的示例时,它最终将其设置为当前的 DateTime。
So what I did was set a temporary value and then set it after the reload. It does make it do an animated effect, but it works. If you know a better way, let me know...
所以我所做的是设置一个临时值,然后在重新加载后设置它。它确实使它产生动画效果,但它有效。如果你知道更好的方法,请告诉我......
func dateChanged(sender: UIDatePicker) {
print(sender.date.description)
let tempDate = sender.date
let currentDate = NSDate()
if originalDate.isLessThanDate(currentDate) {
originalDate = sender.date
if sender.date.isGreaterThanDate(currentDate) {
sender.setValue(UIColor.blackColor(), forKeyPath: "textColor")
sender.datePickerMode = .CountDownTimer
sender.datePickerMode = .DateAndTime
sender.date = tempDate
sender.reloadInputViews()
}
}
if sender.date.isLessThanDate(currentDate) {
sender.setValue(UIColor.redColor(), forKeyPath: "textColor")
sender.datePickerMode = .CountDownTimer
sender.datePickerMode = .DateAndTime
sender.date = tempDate
sender.reloadInputViews()
}
}