- (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];//初始化目標(biāo)時間...
NSDate*today = [NSDatedate];//得到當(dāng)前時間
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];//把目標(biāo)時間裝載入date
//用來得到具體的時差,是為了統(tǒng)一成北京時間
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) {
//計時尚未結(jié)束泌豆,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){
//計時尚結(jié)束终议,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;
}