Change UIDatePicker font color?

后端 未结 15 1733
南方客
南方客 2020-12-04 19:12

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 enti

相关标签:
15条回答
  • 2020-12-04 20:13

    Next solution comes from "arturgrigor" and it works great in my apps, just copy it, paste it in viewDidLoad method, and enjoy it :

    [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];
    
    0 讨论(0)
  • 2020-12-04 20:14

    I stumbled upon a surprisingly clean solution using UIAppearance, without using any KVC, swizzling, or otherwise private API. I found that attempting to set the textColor via UIAppearance for any UILabel within a UIDatePicker had no affect, but a custom appearance property that simply called the regular textColor setter worked just fine.

    // 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
    

    And then use as follows:

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

    Swift Version

    UILabel extension:

    extension UILabel {
        @objc dynamic var textColorWorkaround: UIColor? {
            get {
                return textColor
            }
            set {
                textColor = newValue
            }
        }
    }
    

    Appearance proxy use:

    let pickerLabelProxy = UILabel.appearance(whenContainedInInstancesOf: [UIDatePicker.self])
    pickerLabelProxy.textColorWorkaround = UIColor.lightGray
    
    0 讨论(0)
  • 2020-12-04 20:14
    [date_picker setValue:textColor forKey:@"textColor"];
    [date_picker performSelector:@selector(setHighlightsToday:) withObject:NO];
    
    0 讨论(0)
提交回复
热议问题