简单写了一下用NSDateFormatter
获取系统的时间
先看一下效果图
AM是表示上下的时间
08:32 表示系统的时间
小太阳是一个UIImageView 就不说了
接下来上关键代码
UIView *timeview=[[UIView alloc]initWithFrame:CGRectMake(12, 20, 100, 30)];
对两个UIlabel进行初始化
AMlab=[[UILabel alloc]initWithFrame:CGRectMake(35, 0, 50, 20)];
[AMlab setFont:[UIFont systemFontOfSize:13]];
[AMlab setTextColor:[UIColor blackColor]];
[AMlab setHighlightedTextColor:[UIColor blackColor]];
//
labtime=[[UILabel alloc]initWithFrame:CGRectMake(35 ,21, 50, 9)];
labtime.textColor=[UIColor darkGrayColor];
labtime.font=[UIFont systemFontOfSize:9];
[timeview addSubview:AMlab];
[timeview addSubview:labtime];
//添加两个NSTimer定时器
TimeNow=[NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(cicktimenow) userInfo:nil repeats:YES];
AMtime = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(setTimeNow) userInfo:nil repeats:YES];
//接下来获取变化的时间
-(void)cicktimenow
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSString *timestamp = [formatter stringFromDate:[NSDate date]];
[labtime setText:timestamp];//时间在变化的语句
}
//对于上下午的变化
-(void)setTimeNow
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formatter setDateFormat:@"a."];
NSString *ampm = [formatter stringFromDate:[NSDate date]];
[AMlab setText:ampm];
}
这样就可以简单获取了
网友评论