Change UIDatePicker font color?

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

提交回复
热议问题