1. 程式人生 > >UIDatePicker 修改字型顏色

UIDatePicker 修改字型顏色

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.0green:127 / 255.0blue:127 / 255.0alpha:1];

NSTimeInterval secondPerDay = 24 * 60 *

60;

NSTimeInterval secondPer2Year = secondPerDay * 356 * 2;

NSDate *today = [[NSDatealloc] init];

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

NSDate *min = [today dateByAddingTimeInterval: 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([UIDatePicker

class], &outCount);

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

{

// 迴圈獲取屬性的名字 property_getName函式返回一個屬性的名稱

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

NSLog(@"%@",getPropertyName);

if([getPropertyName isEqualToString:@"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:&no atIndex:2];

[invocation invokeWithTarget:_datePicker];

// 註冊當datepicker值改變時觸發事件

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

[self.viewaddSubview: _datePicker];