倒计时,

- (void)timerFireMethod:(NSTimer *)timer

{

    NSDate* now = [NSDate date];

    NSCalendar *cal = [NSCalendar currentCalendar];

    

    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit;

    NSDateComponents *dd = [cal components:unitFlags fromDate:now];

    int y = [dd year];

    int m = [dd month];

    int d = [dd day];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [[NSDateComponents alloc] init];

    [components setYear:y];

    [components setMonth:m];

    [components setDay:d];

    [components setHour:18];

    [components setMinute:30];

    [components setSecond:0];

    NSDate *fireDate = [calendar dateFromComponents:components];//标时间

    NSDateComponents *dComponent = [calendar components:unitFlags fromDate:now toDate:fireDate options:0];//计算时间差

//    self.hourminutesecondLabel

    

    NSMutableAttributedString *mutiStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%d:%d:%d",[dComponent hour],[dComponent minute],[dComponent second]]];

    NSString *secondLen = [NSString stringWithFormat:@"%d",[dComponent second]];

    [self.hourminutesecondLabel setText:mutiStr afterInheritingLabelAttributesAndConfiguringWithBlock:

     

     ^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {

         

         [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)RGBCOLOR(6, 193, 174) range:NSMakeRange(0, 3)];

         [mutableAttributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:39.0] range:NSMakeRange(mutiStr.length - secondLen.length, secondLen.length)];

 

         return mutableAttributedString;//需要有返回值的,

         

     }];

    if ([dComponent second] < 0) {

        self.hourminutesecondLabel.text = @"下班啦";

    }

   

 

}

原文地址:https://www.cnblogs.com/guligei/p/3941716.html