小编在开发的过程中,遇到这一需求,实在是让我头疼半天,因为UIdatePicker 没有更改字体的属性设置,当时找遍了百度上的很多方法,都不是很靠谱,最后通过朋友了解到runtime的相关知识,于是将这一问题解决,好了废话少,马上为您,奉上代码。
以下是在.h文件中的声明
#import <UIKit/UIKit.h>
@protocol XG_datePickerDelegate <NSObject>
@optional
-(void)chooseDateString:(NSString*)dateString;
@end
@interface DataPcikerView : UIView
@property (nonatomic, assign) id<XG_datePickerDelegate> delegate;
@end
#import "DataPcikerView.h"
#import <objc/runtime.h>
#define kUIColor(a,b,c) [UIColor colorWithRed:a/255.0 green:b/255.0 blue:c/255.0 alpha:1]
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define buttonW 50
#define buttonH 40
#define spaceX 10
@implementation DataPcikerView
{
NSString * _dateString;
UIDatePicker *picker;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self .backgroundColor= kUIColor(56, 62, 72);
[self createUI];
}
return self;
}
-(void)createUI
{
//日期
picker=[[UIDatePicker alloc] initWithFrame:CGRectMake(0,40, WIDTH,self.frame.size.height*3/4)];
//设置白色字体
unsigned int outCount;
int i;
objc_property_t *pProperty = class_copyPropertyList([UIDatePicker class], &outCount);
for (i = outCount -1; i >= 0; i--)
{
// 循环获取属性的名字 property_getName函数返回一个属性的名称
NSString *getPropertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
NSString *getPropertyNameString = [NSString stringWithCString:property_getAttributes(pProperty[i]) encoding:NSUTF8StringEncoding];
if([getPropertyName isEqualToString:@"textColor"])
{
[picker setValue:[UIColor whiteColor] forKey:@"textColor"];
}
NSLog(@"%@====%@",getPropertyNameString,getPropertyName);
}
SEL selector = NSSelectorFromString(@"setHighlightsToday:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]];
BOOL no = NO;
[invocation setSelector:selector];
[invocation setArgument:&no atIndex:2];
[invocation invokeWithTarget:picker];
picker.backgroundColor= kUIColor(56, 62, 72);
// picker.backgroundColor=kUIColor(238, 88, 39);
// picker.backgroundColor= deepBgColor;
// picker.layer.borderColor=[kUIColor(238, 238,238) CGColor];
// picker.layer.borderWidth=1;
[picker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
picker.locale=locale;
NSDate *localDate=[NSDate date];
NSCalendar *gregorion=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents=[[NSDateComponents alloc] init];
//设置时间
[offsetComponents setYear:0];
[offsetComponents setMonth:0];
[offsetComponents setDay:0];
//设置最大的时间
NSDate *maxDate=[gregorion dateByAddingComponents:offsetComponents toDate:localDate options:0];
picker.maximumDate=maxDate;
//设置日期的显示格式
picker.datePickerMode=UIDatePickerModeDate;
[self addSubview:picker];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.frame = CGRectMake(WIDTH - buttonW - spaceX,0, buttonW, buttonH);
[btn setTitle:@"确定" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(dateBtn:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn1.frame = CGRectMake(spaceX,0, buttonW, buttonH);
[btn1 setTitle:@"取消" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(dissMissDate) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn1];
}
-(void)dissMissDate
{
[UIView animateWithDuration:1 animations:^{
self.frame = CGRectMake(0, HEIGHT, WIDTH, self.frame.size.height);
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
-(void)dateBtn:(UIButton*)btn
{
if (_dateString.length>0) {
if ([self.delegate respondsToSelector:@selector(chooseDateString:)]) {
[self.delegate chooseDateString:_dateString];
}
}
}
-(void)dateChange:(UIDatePicker *)pick
{
NSDate *pickDate=[pick date];
NSDateFormatter *pickerFormatter=[[NSDateFormatter alloc] init];
[pickerFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dataStr=[pickerFormatter stringFromDate:pickDate];
NSLog(@"----------dataStr-------%@",dataStr);
_dateString = dataStr;
}
objc_property_t *pProperty = class_copyPropertyList([UIDatePicker class], &outCount);
for (i = outCount -1; i >= 0; i--)
{
// 2 循环获取属性的名字 property_getName函数返回一个属性的名称
NSString *getPropertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
NSString *getPropertyNameString = [NSStringstringWithCString:property_getAttributes(pProperty[i]) encoding:NSUTF8StringEncoding];
// 3、 查找到textcolor的方法 设置这个方法的value值
if([getPropertyName isEqualToString:@"textColor"])
{
[picker setValue:[UIColor whiteColor] forKey:@"textColor"];
}
NSLog(@"%@====%@",getPropertyNameString,getPropertyName);
}
// 3、 查找到@"setHighlightsToday:" 的方法 设置这个方法的value值
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePickerinstanceMethodSignatureForSelector:selector]];
BOOL no = NO;
[invocation setSelector:selector];
[invocation setArgument:&no atIndex:2];// 这个方法要从这里设置参数的Index 需要从2开始,因为前两个被selector和target占用
[invocation invokeWithTarget:picker];// 设好的参数 以及方法 赋值 给pickerView