- (id)initTerminalWithFrame:(CGRect)frame startTime:(NSInteger)startTime type:(TimeStyle)type {
self= [superinitWithFrame:frame];
if(self) {
self.style= type;
self.startTime= startTime;
self.timeLb= [[UILabelalloc]initWithFrame:self.bounds];
self.timeLb.text=self.style==kDefaultStyle?@"00 : 00 : 00":@"00天00 : 00 : 00";
self.timeLb.adjustsFontSizeToFitWidth=YES;
self.layerViews= [NSMutableArrayarray];
NSIntegerlayerNumber =self.style==kDefaultStyle?3:4;
CGFloatlayerW =self.style==kDefaultStyle?self.bounds.size.width/4.5:self.bounds.size.width/6.0;
CGFloatmargin = (self.bounds.size.width- layerNumber * layerW)/(layerNumber *1.0-1);
CGFloatstartX =0;
for(inti =0; i < layerNumber; i ++) {
if(self.style==kDayStyle) {
startX += i ==1? layerW + margin +5: layerW + margin -3;
}else{
startX += layerW + margin;
}
if(i ==0) {
startX =0;
}
UIView*layer = [[UIViewalloc]initWithFrame:CGRectMake(startX,self.bounds.size.height*0.25, layerW,self.bounds.size.height*0.5)];
[selfaddSubview:layer];
[self.layerViewsaddObject:layer];
}
[selfaddSubview:self.timeLb];
self.timer= [NSTimerscheduledTimerWithTimeInterval:1.0target:selfselector:@selector(timerFireMethod:)userInfo:nilrepeats:YES];
}
returnself;
}
- (void)setTextColor:(UIColor*)textColor {
if(self.textColor!= textColor) {
_textColor= textColor;
self.timeLb.attributedText= [selfattributedStringWithText:self.timeLb.text];
}
}
- (void)setLayerColor:(UIColor*)layerColor {
if(self.layerColor!= layerColor) {
_layerColor= layerColor;
for(UIView*subVinself.layerViews) {
subV.backgroundColor= layerColor;
}
}
}
- (void)setTimeColor:(UIColor*)timeColor {
if(self.timeColor!= timeColor) {
_timeColor= timeColor;
self.timeLb.textColor= timeColor;
}
}
- (void)setTextFont:(UIFont*)textFont {
if(self.textFont!= textFont) {
_textFont= textFont;
self.timeLb.font= textFont;
}
}
staticboolisCaculating =YES;
- (void)timerFireMethod:(NSTimer*)theTimer {
NSCalendar*cal = [NSCalendarcurrentCalendar];//定义一个NSCalendar对象
NSDateComponents*endTime = [[NSDateComponentsalloc]init];//初始化目标时间...
NSDate*today = [NSDatedate];//得到当前时间
NSDate*date = [NSDatedateWithTimeIntervalSince1970:self.startTime];
NSDateFormatter*dateFormatter = [[NSDateFormatteralloc]init];
[dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone*timeZone = [NSTimeZonetimeZoneWithName:@"Asia/Shanghai"];
[dateFormattersetTimeZone:timeZone];
NSString*dateString = [dateFormatterstringFromDate:date];
staticintyear;
staticintmonth;
staticintday;
staticinthour;
staticintminute;
staticintsecond;
year = [[dateStringsubstringWithRange:NSMakeRange(0,4)]intValue];
month = [[dateStringsubstringWithRange:NSMakeRange(5,2)]intValue];
day = [[dateStringsubstringWithRange:NSMakeRange(8,2)]intValue];
hour = [[dateStringsubstringWithRange:NSMakeRange(11,2)]intValue];
minute = [[dateStringsubstringWithRange:NSMakeRange(14,2)]intValue];
second = [[dateStringsubstringWithRange:NSMakeRange(17,2)]intValue];
[endTimesetYear:year];
[endTimesetMonth:month];
[endTimesetDay:day];
[endTimesetHour:hour];
[endTimesetMinute:minute];
[endTimesetSecond:second];
NSDate*todate = [caldateFromComponents:endTime];//把目标时间装载入date
//用来得到具体的时差,是为了统一成北京时间
unsignedintunitFlags =NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond;
NSDateComponents*d = [calcomponents:unitFlagsfromDate:todaytoDate:todateoptions:0];
NSString*fen = [NSStringstringWithFormat:@"%ld", [dminute]];
if([dminute] <10) {
fen = [NSStringstringWithFormat:@"0%ld",[dminute]];
}
NSString*miao = [NSStringstringWithFormat:@"%ld", [dsecond]];
if([dsecond] <10) {
miao = [NSStringstringWithFormat:@"0%ld",[dsecond]];
}
if([dsecond] >=0) {
//计时尚未结束,do_something
self.timeLb.text=self.style==kDefaultStyle?
[NSStringstringWithFormat:@"%02ld : %02ld : %02ld",d.hour,d.minute,d.second]
: [NSStringstringWithFormat:@"%02ld天%02ld : %02ld : %02ld",d.day,d.hour,d.minute,d.second];
}elseif([dsecond] ==0){
//计时尚结束,do_something
self.timeLb.text=self.style==kDefaultStyle?
[NSStringstringWithFormat:@"%02ld : %02ld : %02ld",d.hour,d.minute,d.second]
: [NSStringstringWithFormat:@"%02ld天%02ld : %02ld : %02ld",d.day,d.hour,d.minute,d.second];
}else{
[theTimerinvalidate];
self.timeLb.text=self.style==kDefaultStyle?
@"00 : 00 : 00":@"00天00 : 00 : 00";
}
self.timeLb.attributedText= [selfattributedStringWithText:self.timeLb.text];
}
- (void)changeAttributedOtherStringFontAndColorInRange:(NSRange)range attributedString:(NSMutableAttributedString*)attributedStr {
[attributedStraddAttribute:NSForegroundColorAttributeNamevalue:self.textColorrange:range];
}
- (NSMutableAttributedString*)attributedStringWithText:(NSString*)text {
NSMutableAttributedString*attributedString = [[NSMutableAttributedStringalloc]initWithString:text];
CGFloatstartLocation =2;
[selfchangeAttributedOtherStringFontAndColorInRange:NSMakeRange(startLocation,3)attributedString:attributedString];
[selfchangeAttributedOtherStringFontAndColorInRange:NSMakeRange(startLocation +5,3)attributedString:attributedString];
if(self.style==kDayStyle) {
[selfchangeAttributedOtherStringFontAndColorInRange:NSMakeRange(startLocation +10,3)attributedString:attributedString];
}
returnattributedString;
}