Change UIDatePicker font color?

后端 未结 15 1731
南方客
南方客 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 19:51

    For Xamarin developers:

    DatePicker.SetValueForKey(UIColor.White, new NSString("textColor"));
    DatePicker.SetValueForKey(FromObject(false), new NSString("highlightsToday"));
    

    It´s working like a charm. Tested in iOS 9 and 10

    0 讨论(0)
  • 2020-12-04 19:53

    You can also add this as an IBDesignable if you want to configure this within InterFace Builder.

        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. 
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 19:54

    This subclass of UIDatePicker works for iOS 7. Its not pretty but gets the job done.

    #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
    
    0 讨论(0)
  • 2020-12-04 20:01

    According to Apple's UIKit User Interface Catalog, developers are not allowed to customize date pickers.

    I've seen other StackOverflow answers for similar questions that suggest making a fake UIDatePicker using UIPickerView and customizing that.

    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.

    0 讨论(0)
  • 2020-12-04 20:03

    All I need (on iOS 8.x and 9.0) is this one line to change the font color:

            [my_date_picker setValue:[UIColor whiteColor] forKey:@"textColor"];
    

    No subclassing or invoking of private APIs...


    Note: today's current date will still be highlighted (in newer iOS versions). You can get around this by using the following code:

    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
    
    }
    
    0 讨论(0)
  • As of 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,

    0 讨论(0)
提交回复
热议问题