UIDatePicker 修改字体颜色

来源:互联网 发布:淘宝商城冬季女鞋 编辑:程序博客网 时间:2024/06/16 09:42

self.datePicker = [[UIDatePickeralloc] init];

    self.datePicker.frame =CGRectMake(0,200, self.view.frame.size.width,300);

    _datePicker.center =self.view.center;

    

    self.datePicker.backgroundColor = [UIColorcolorWithRed:127 /255.0 green:127 /255.0 blue:127 /255.0 alpha:1];

    

    NSTimeInterval secondPerDay =24 * 60 *60;

    NSTimeInterval secondPer2Year = secondPerDay *356 * 2;

    

    NSDate *today = [[NSDatealloc] init];

    //    NSDate *max = [today dateByAddingTimeInterval: -secondPer2Year];

    NSDate *min = [todaydateByAddingTimeInterval: 0];

    

    // 设置为中文

    [_datePickersetLocale: [[NSLocalealloc] initWithLocaleIdentifier:@"zh_CN"]];

    // 设置日期选择器模式

    [_datePickersetDatePickerMode: UIDatePickerModeDate];

    // 设置默认选中日期

    [_datePickersetDate: today animated:YES];

    // 设置允许选择的最小日期

    [_datePickersetMinimumDate: min];

    // 设置允许选择的最大日期

    //    [_datePicker setMaximumDate: max];

    

 

    unsignedint outCount;

    int i;

    objc_property_t *pProperty =class_copyPropertyList([UIDatePickerclass], &outCount);

    for (i = outCount -1; i >=0; i--)

    {

        // 循环获取属性的名字  property_getName函数返回一个属性的名称

        NSString *getPropertyName = [NSStringstringWithCString:property_getName(pProperty[i])encoding:NSUTF8StringEncoding];

        NSLog(@"%@",getPropertyName);

        if([getPropertyNameisEqualToString:@"textColor"])

        {

            [_datePickersetValue:[UIColorwhiteColor] forKey:@"textColor"];

        }

    }

    ///修改最大最小时间颜色的BUG

    SEL selector =NSSelectorFromString(@"setHighlightsToday:");

     //NSInvocation;用来包装方法和对应的对象,它可以存储方法的名称,对应的对象,对应的参数,

    NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:[UIDatePickerinstanceMethodSignatureForSelector:selector]];

    BOOL no =NO;

    [invocation setSelector:selector];

    //注意:设置参数的索引时不能从0开始,因为0已经被self占用,1已经被_cmd占用

    [invocation setArgument:&noatIndex:2];

    [invocation invokeWithTarget:_datePicker];


    

    // 注册当datepicker值改变时触发事件

    [_datePickeraddTarget: selfaction: @selector(datePickerChanged:)forControlEvents:UIControlEventValueChanged];

    [self.viewaddSubview: _datePicker];

原创粉丝点击