#import <UIKit/UIKit.h>
//typedef void(^DatePickerTimerBlock)(DatePickerView * date,NSString *str);
@interface DatePickerView : UIView
@property(nonatomic, copy)void(^datePickerTimeBlock)( DatePickerView *date, NSString * str);
@end
#import "DatePickerView.h"
@interface DatePickerView()
/**
* 日期控件
*/
@property (nonatomic, weak) UIDatePicker *datePicker;
@end
@implementation DatePickerView
/**
* 初始化控件
*
* @return
*/
- (UIDatePicker *)datePicker
{
if (!_datePicker) {
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
//设置最大的选择日期
// datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:3600];
[datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
[self addSubview:datePicker];
_datePicker = datePicker;
}
return _datePicker;
}
- (void)setDatePickerTimeBlock:(void (^)(DatePickerView *, NSString *))datePickerTimeBlock
{
}
/**
* 日期 改变了
*
* @return
*/
- (void)dateChange:(UIDatePicker *)picker
{
//1.获取当前选中的时间
NSDate *date = picker.date;
//2.将date 转化为字符串
NSString *dateString = [self stringFormate:date];
if (self.datePickerTimeBlock) {
self.datePickerTimeBlock(self,dateString);
}
NSLog(@"%@",dateString);
}
/**
* 将时间转化为字符串
*
* @return
*/
- (NSString *)stringFormate:(NSDate *)date
{
//初始化日期格式类
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"yyyy-MM-dd";
//将date 装化为字符串
return [formatter stringFromDate:date];
}
- (void)layoutSubviews
{
[super layoutSubviews];
//设置坐标
self.datePicker.frame = self.bounds;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
#import "ViewController.h"
#import "DatePickerView.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@end
@implementation ViewController
- (IBAction)btnClick:(id)sender {
DatePickerView *datePicker = [[DatePickerView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 216)];
//动画
[UIView animateWithDuration:0.25 animations:^{
CGRect frame = datePicker.frame;
frame.origin.y = self.view.frame.size.height - 216;
datePicker.frame = frame;
}];
datePicker.backgroundColor = [UIColor cyanColor];
[self.view addSubview:datePicker];
[datePicker setDatePickerTimeBlock:^(DatePickerView * date, NSString *str) {
self.dateLabel.text = str;
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
/**
* 如何让一个Label上显示两种颜色 或者两种不同颜色的字体
我们可以用两种方式实现
1.使用CoreText 这种方式偏向 底层 实现起来较为复杂
2.使用属性字符串
*
* @return <#return value description#>
*/
[self show];
[self showDate];
[self numberFormater];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)showDate
{
/**
* 日历类
*
* @return <#return value description#>
*/
NSCalendar *calendar = [NSCalendar currentCalendar];
/**
* 获取系统当前时间
*
* @return <#return value description#>
*/
NSDate *date = [NSDate date];
/**
* 日期组件
*
* 保存 年月日时分秒周
*/
/**
*
NSCalendarUnitEra = kCFCalendarUnitEra,
NSCalendarUnitYear = kCFCalendarUnitYear,
NSCalendarUnitMonth = kCFCalendarUnitMonth,
NSCalendarUnitDay = kCFCalendarUnitDay,
NSCalendarUnitHour = kCFCalendarUnitHour,
NSCalendarUnitMinute = kCFCalendarUnitMinute,
NSCalendarUnitSecond = kCFCalendarUnitSecond,
NSCalendarUnitWeekday = kCFCalendarUnitWeekday,
NSCalendarUnitWeekdayOrdinal = kCFCalendarUnitWeekdayOrdinal,
*
* @param NSCalendarUnit <#NSCalendarUnit description#>
*
* @return <#return value description#>
*/
// NSDateComponents *componets = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday fromDate:date];
NSDateComponents *componets = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday fromDate:date];
/*
周日:1
周一:2
*/
NSLog(@"%ld/%ld/%ld 周%ld",componets.year,componets.month,componets.day,componets.weekday - 1);
NSDate *date1 = [NSDate date];
NSDate *date2 = [NSDate dateWithTimeInterval:3600 sinceDate:date];
/**
* 1.两个时间相差多少秒 ABS
*
* @return <#return value description#>
*/
[date1 timeIntervalSinceDate:date2];
/**
* 2.date 距离1970年1月1号的秒数
*
* @return <#return value description#>
*/
NSTimeInterval seconds = [date1 timeIntervalSince1970];
[date1 timeIntervalSinceReferenceDate];
/**
* 指定距离现在时间差多少秒
*
* @return <#return value description#>
*/
[date1 timeIntervalSinceNow];
}
- (void)show
{
/**
* 属性字符串
*
* @return <#return value description#>
*/
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString: @"Can you see me ?"];
/**
* 指定范围添加属性
*
* @return <#return value description#>
*/
[attributeString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:40],NSForegroundColorAttributeName:[UIColor redColor]} range:NSMakeRange(0, 1)];
[attributeString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor lightGrayColor]} range:NSMakeRange(1, 5)];
[attributeString addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25],NSForegroundColorAttributeName:[UIColor greenColor]} range:NSMakeRange(5, 10)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, 200, 50)];
label.attributedText = attributeString;
[self.view addSubview:label];
}
- (void)numberFormater
{
/**
* 数字格式化
*
* @return
*/
//在我们做项目的过程中 我们会遇到这样一个问题
//将一个数字字符串 或者 整形 转化为指定的格式
//例如电话号码178-9876-7489
//金额 $1,983,234,234,987
//将数字转化为大写
//如果我们自己封装类是比较麻烦的 发现一个很好的方法推荐给大家
//这个是 苹果给我们提供的一个数字格式化的类 这个类相当的强大
/**
我们可以使用 系统提供给我们的一些方法 这个就不挨个实验了
typedef NS_ENUM(NSUInteger, NSNumberFormatterStyle) {
NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle,
};
*/
NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
/**
* 设置样式
*
* @return
*/
formatter.numberStyle = kCFNumberFormatterDecimalStyle;
NSString *str = [formatter stringFromNumber:@(12973496236)];
NSLog(@"%@",str);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end