我一直在尝试更改iOS中的原生UIDatePicker中的字体而且我做了,但有一些未解决的细节:
我使用UIDatePicker扩展来更改其标签中的字体:
extension UIDatePicker {
func stylizeView(view: UIView? = nil) {
let view = view ?? self
for subview in view.subviews {
if let label = subview as? UILabel {
if let text = label.text {
print("UIDatePicker :: sylizeLabel :: \(text)\n")
label.font = UIFont(name: "MyriadPro-Light", size: 17)!
}
} else { stylizeView(subview) }
}
}}
因此,您可以深入自定义字体:
struct DatePickerStyle {
let tintColor = UIColor(hex: 0xFFFFFF)
let font = UIFont(name: "MyriadPro-Light", size: 17)!
let fontColor = UIColor(hex: 0x000000)
let fontKern: CGFloat = 0.2
var paragraphStyle: NSMutableParagraphStyle {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 4
paragraphStyle.lineHeightMultiple = 1
paragraphStyle.alignment = .Right
return paragraphStyle
}}
extension UIDatePicker {
func stylizeView(view: UIView? = nil) {
let style = DatePickerStyle()
let view = view ?? self
for subview in view.subviews {
if let label = subview as? UILabel {
if let text = label.text {
print("UIDatePicker :: sylizeLabel :: \(text)\n")
let attributedString = NSMutableAttributedString(string: text)
let attributedStringRange = NSMakeRange(0, attributedString.length)
attributedString.addAttributes([
NSParagraphStyleAttributeName: style.paragraphStyle,
NSFontAttributeName: style.font,
NSForegroundColorAttributeName: style.fontColor,
NSKernAttributeName: style.fontKern
], range: attributedStringRange)
//label.font = style.font
label.tintColor = style.fontColor
label.attributedText = attributedString
}
} else { stylizeView(subview) }
}
}
}
此扩展功能在UIDatePicker的任何控制事件上实现:
datePicker.addTarget(self, action: #selector(CellWithDatePicker.updateDatePickerStyle), forControlEvents: .AllEvents)
&安培;
func updateDatePickerStyle() {
print(":: updateDatePickerStyle")
datePicker.stylizeView()
}
问题1:
当我初始化UIDatePicker时,选择器的字体仍然是SanFrancisco。
但是当我在UIDatePicker中更改值时,字体会更改为我的字体
我尝试在UIDatePicker生命周期的每个阶段实现 datePicker.stylizeView()或 self.stylizeView(),但它只能更改选定的DatePicker行。< / p>
问题2:
当我在使用newFont设置DatePicker的所有标签时,在pic.2之后旋转DatePicker时,所选行之外的新标签仍然是旧字体(SanFrancisco)。当我停止旋转DatePicker时,所有标签都更新为newFont。
知道怎么解决吗?
答案 0 :(得分:0)
使用间隔为0.1的GCD调用样式函数。
示例:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1)
{
self.datePicker.stylizeView();ss
}
答案 1 :(得分:0)
更改UIDatePickerView字体(直到现在)的唯一方法是麻烦:
您可以通过扩展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
}
}
要更改颜色,只需调用以下函数:
datePicker.setValue(UIColor.whiteColor(), forKeyPath: "textColor")
如果有必要重新渲染,则需要致电:
datePicker.datePickerMode = .CountDownTimer
datePicker.datePickerMode = .DateAndTime //or whatever your original mode was