iOS 开发UidatePicker 改变字体颜色的方法
来源:互联网 发布:mac cad怎么样卸载软件 编辑:程序博客网 时间:2024/06/05 06:28
小编在开发的过程中,遇到这一需求,实在是让我头疼半天,因为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 = [superinitWithFrame:frame]) {
self .backgroundColor=kUIColor(56,62, 72);
[selfcreateUI];
}
returnself;
}
-(void)createUI
{
//日期
picker=[[UIDatePickeralloc] initWithFrame:CGRectMake(0,40,WIDTH,self.frame.size.height*3/4)];
//设置白色字体
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];
NSString *getPropertyNameString = [NSStringstringWithCString:property_getAttributes(pProperty[i])encoding:NSUTF8StringEncoding];
if([getPropertyNameisEqualToString:@"textColor"])
{
[pickersetValue:[UIColorwhiteColor] forKey:@"textColor"];
}
NSLog(@"%@====%@",getPropertyNameString,getPropertyName);
}
SEL selector =NSSelectorFromString(@"setHighlightsToday:");
NSInvocation *invocation = [NSInvocationinvocationWithMethodSignature:[UIDatePickerinstanceMethodSignatureForSelector:selector]];
BOOL no =NO;
[invocation setSelector:selector];
[invocation setArgument:&noatIndex: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;
[pickeraddTarget:selfaction:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
NSLocale *locale=[[NSLocalealloc] initWithLocaleIdentifier:@"zh_CN"];
picker.locale=locale;
NSDate *localDate=[NSDatedate];
NSCalendar *gregorion=[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents=[[NSDateComponentsalloc] init];
//设置时间
[offsetComponents setYear:0];
[offsetComponents setMonth:0];
[offsetComponents setDay:0];
//设置最大的时间
NSDate *maxDate=[gregoriondateByAddingComponents:offsetComponentstoDate:localDate options:0];
picker.maximumDate=maxDate;
//设置日期的显示格式
picker.datePickerMode=UIDatePickerModeDate;
[selfaddSubview:picker];
UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];
[btn setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];
btn.frame =CGRectMake(WIDTH -buttonW - spaceX,0,buttonW, buttonH);
[btn setTitle:@"确定"forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(dateBtn:)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:btn];
UIButton *btn1=[UIButtonbuttonWithType:UIButtonTypeCustom];
[btn1 setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];
btn1.frame =CGRectMake(spaceX,0,buttonW, buttonH);
[btn1 setTitle:@"取消"forState:UIControlStateNormal];
[btn1 addTarget:selfaction:@selector(dissMissDate)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:btn1];
}
-(void)dissMissDate
{
[UIViewanimateWithDuration:1animations:^{
self.frame =CGRectMake(0,HEIGHT, WIDTH,self.frame.size.height);
} completion:^(BOOL finished) {
[selfremoveFromSuperview];
}];
}
-(void)dateBtn:(UIButton*)btn
{
if (_dateString.length>0) {
if ([self.delegaterespondsToSelector:@selector(chooseDateString:)]) {
[self.delegatechooseDateString:_dateString];
}
}
}
-(void)dateChange:(UIDatePicker *)pick
{
NSDate *pickDate=[pickdate];
NSDateFormatter *pickerFormatter=[[NSDateFormatteralloc] init];
[pickerFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dataStr=[pickerFormatterstringFromDate: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
- iOS 开发UidatePicker 改变字体颜色的方法
- IOS UIdatePicker 改变背景颜色
- Android开发笔记 改变字体颜色的三种方法
- iOS改变状态栏的字体颜色
- iOS 改变状态栏的字体颜色
- 利用Runtime修改UIdatePicker的字体颜色
- CSS使字体颜色改变的方法
- 改变navigation title 字体颜色的方法
- UIDatePicker 修改字体颜色
- iOS改变状态栏字体颜色
- iOS开发---改变UINavigationBar导航条标题颜色和字体
- iOS开发用Tableview实现能改变字体颜色的高仿系统的ActionSheet
- iOS一种简单的改变一个label上范围内字体颜色和大小的方法
- iOS开发中,在label中改变数字颜色的方法(改变某个关键词的方法)
- ios 改变状态栏颜色以及字体颜色
- iOS如何改变所有界面的状态栏的字体颜色
- ios:改变tableview的titleForHeaderInSection字体颜色设置
- [iOS]UILabel改变某个范围的字符颜色和字体
- 关于NSString赋值retainCount等于-1
- Android 打造形形色色的进度条 实现可以如此简单
- java运行原理
- 关于UITableView使用
- 戴尔Vostro成就3901如何安装win10系统
- iOS 开发UidatePicker 改变字体颜色的方法
- php语言
- Hadoop源代码分析(九)
- 建表参考
- 用美图秀秀(微软调色版)生成的R分量为255的图片在MATLAB和OpenCV中R分量为254
- 数据库了解及MySQL学习(持续更新)
- 字符串MD5加密
- centos安装nginx全过程 备份
- mysql索引总结----mysql 索引类型以及创建