DatePicker Text Color Change(TEXT WHITE & BG BLACK)

LINKS:-

http://stackoverflow.com/questions/18807940/can-i-change-the-font-color-of-the-datepicker-in-ios7

 

.h

UILabel+WhiteUIDatePickerLabels.h

.m

#import “UILabel+WhiteUIDatePickerLabels.h”

#import <objc/runtime.h>

@implementation UILabel (WhiteUIDatePickerLabels)

+ (void)load {

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        [self swizzleInstanceSelector:@selector(setTextColor:)

                      withNewSelector:@selector(swizzledSetTextColor:)];

        [self swizzleInstanceSelector:@selector(willMoveToSuperview::)

                      withNewSelector:@selector(swizzledWillMoveToSuperview:)];

    });

}

// Forces the text colour of the label to be white only for UIDatePicker and its components

-(void) swizzledSetTextColor:(UIColor *)textColor {

    if([self view:self hasSuperviewOfClass:[UIDatePicker class]] ||

       [self view:self hasSuperviewOfClass:NSClassFromString(@”UIDatePickerWeekMonthDayView”)] ||

       [self view:self hasSuperviewOfClass:NSClassFromString(@”UIDatePickerContentView”)]){

        [self swizzledSetTextColor:[UIColor whiteColor]];

    } else {

        //Carry on with the default

        [self swizzledSetTextColor:textColor];

    }

}

// Some of the UILabels haven’t been added to a superview yet so listen for when they do.

– (void) swizzledWillMoveToSuperview:(UIView *)newSuperview {

    [self swizzledSetTextColor:self.textColor];

    [self swizzledWillMoveToSuperview:newSuperview];

}

// — helpers —

– (BOOL) view:(UIView *) view hasSuperviewOfClass:(Class) class {

    if(view.superview){

        if ([view.superview isKindOfClass:class]){

            return true;

        }

        return [self view:view.superview hasSuperviewOfClass:class];

    }

    return false;

}

+ (void) swizzleInstanceSelector:(SEL)originalSelector

                 withNewSelector:(SEL)newSelector

{

    Method originalMethod = class_getInstanceMethod(self, originalSelector);

    Method newMethod = class_getInstanceMethod(self, newSelector);

    

    BOOL methodAdded = class_addMethod([self class],

                                       originalSelector,

                                       method_getImplementation(newMethod),

                                       method_getTypeEncoding(newMethod));

    

    if (methodAdded) {

        class_replaceMethod([self class],

                            newSelector,

                            method_getImplementation(originalMethod),

                            method_getTypeEncoding(originalMethod));

    } else {

        method_exchangeImplementations(originalMethod, newMethod);

    }

}

@end

ADD CALL JUST ::::::IN VIEW DIDLOAD

    [UILabel load];

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s