Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions MZTimerLabel/MZTimerLabel.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
//
// MZTimerLabel.h
// Version 0.5.1
// Created by MineS Chan on 2013-10-16
// Updated 2014-12-15

// This code is distributed under the terms and conditions of the MIT license.

// Copyright (c) 2014 MineS Chan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// MZTimerLabel.h
// Version 0.5.1
// Created by MineS Chan on 2013-10-16
// Updated 2014-12-15

// This code is distributed under the terms and conditions of the MIT license.

// Copyright (c) 2014 MineS Chan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import <UIKit/UIKit.h>

Expand All @@ -42,22 +42,21 @@ typedef enum{
@optional

- timerLabel:finshedCountDownTimerWithTimeWithTime:
** MZTimerLabel Delegate method for finish of countdown timer

** MZTimerLabel Delegate method for finish of countdown timer
- timerLabel:countingTo:timertype:
** MZTimerLabel Delegate method for monitering the current counting progress
** MZTimerLabel Delegate method for monitering the current counting progress

- timerlabel:customTextToDisplayAtTime:
** MZTimerLabel Delegate method for overriding the text displaying at the time, implement this for your very custom display formmat
**********************************************/
** MZTimerLabel Delegate method for overriding the text displaying at the time, implement this for your very custom display formmat
**********************************************/

@class MZTimerLabel;
@protocol MZTimerLabelDelegate <NSObject>
@optional
-(void)timerLabel:(MZTimerLabel*)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime;
-(void)timerLabel:(MZTimerLabel*)timerLabel countingTo:(NSTimeInterval)time timertype:(MZTimerLabelType)timerType;
-(NSString*)timerLabel:(MZTimerLabel*)timerLabel customTextToDisplayAtTime:(NSTimeInterval)time;
-(NSAttributedString*)timerLabel:(MZTimerLabel*)timerLabel customAttributedTextToDisplayAtTime:(NSTimeInterval)time;
@end

/**********************************************
Expand Down Expand Up @@ -124,6 +123,7 @@ typedef enum{
- (NSTimeInterval)getTimeRemaining;
- (NSTimeInterval)getCountDownTime;

-(void)updateLabelWihDate:(NSDate*)startedDate;



Expand Down
124 changes: 57 additions & 67 deletions MZTimerLabel/MZTimerLabel.m
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
//
// MZTimerLabel.h
// Version 0.5.1
// Created by MineS Chan on 2013-10-16
// Updated 2014-12-15

// This code is distributed under the terms and conditions of the MIT license.

// Copyright (c) 2014 MineS Chan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// MZTimerLabel.h
// Version 0.5.1
// Created by MineS Chan on 2013-10-16
// Updated 2014-12-15

// This code is distributed under the terms and conditions of the MIT license.

// Copyright (c) 2014 MineS Chan
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "MZTimerLabel.h"

Expand All @@ -34,6 +34,7 @@
#define kDefaultFireIntervalNormal 0.1
#define kDefaultFireIntervalHighUse 0.01
#define kDefaultTimerType MZTimerLabelTypeStopWatch

@interface MZTimerLabel(){

NSTimeInterval timeUserValue;
Expand All @@ -57,12 +58,6 @@ @implementation MZTimerLabel

@synthesize timeFormat = _timeFormat;

- (void)dealloc {
if (_timer) {
[_timer invalidate];
}
}

- (id)initWithTimerType:(MZTimerLabelType)theType {
return [self initWithFrame:CGRectZero label:nil andTimerType:theType];
}
Expand Down Expand Up @@ -91,11 +86,11 @@ -(id)initWithFrame:(CGRect)frame label:(UILabel*)theLabel andTimerType:(MZTimerL

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
return self;
}

#pragma mark - Cleanup
Expand Down Expand Up @@ -139,7 +134,7 @@ -(void)setCountDownToDate:(NSDate*)date{
timeToCountOff = [date1970 dateByAddingTimeInterval:0];
}
[self updateLabel];

}

- (void)setTimeFormat:(NSString *)timeFormat{
Expand Down Expand Up @@ -187,7 +182,7 @@ -(void)addTimeCountedByTime:(NSTimeInterval)timeToAdd
}else if (_timerType == MZTimerLabelTypeStopWatch) {
NSDate *newStartDate = [startCountDate dateByAddingTimeInterval:-timeToAdd];
if([[NSDate date] timeIntervalSinceDate:newStartDate] <= 0) {
//prevent less than 0
//prevent less than 0
startCountDate = [NSDate date];
}else{
startCountDate = newStartDate;
Expand Down Expand Up @@ -272,14 +267,14 @@ -(void)startWithEndingBlock:(void(^)(NSTimeInterval))end{
[self start];
}
#endif

-(void)pause{
if(_counting){
[_timer invalidate];
_timer = nil;
_counting = NO;
pausedTime = [NSDate date];
}
if(_counting){
[_timer invalidate];
_timer = nil;
_counting = NO;
pausedTime = [NSDate date];
}
}

-(void)reset{
Expand All @@ -299,7 +294,7 @@ -(void)setup{


-(void)updateLabel{

NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:startCountDate];
NSDate *timeToShow = [NSDate date];
BOOL timerEnded = false;
Expand All @@ -317,18 +312,18 @@ -(void)updateLabel{
if([_delegate respondsToSelector:@selector(timerLabel:countingTo:timertype:)]){
[_delegate timerLabel:self countingTo:timeDiff timertype:_timerType];
}

}else{

/***MZTimerLabelTypeTimer Logic***/
/***MZTimerLabelTypeTimer Logic***/

if (_counting) {

if([_delegate respondsToSelector:@selector(timerLabel:countingTo:timertype:)]){
NSTimeInterval timeLeft = timeUserValue - timeDiff;
[_delegate timerLabel:self countingTo:timeLeft timertype:_timerType];
}

if(timeDiff >= timeUserValue){
[self pause];
timeToShow = [date1970 dateByAddingTimeInterval:0];
Expand All @@ -342,8 +337,8 @@ -(void)updateLabel{
timeToShow = timeToCountOff;
}
}

//setting text value
//setting text value
if ([_delegate respondsToSelector:@selector(timerLabel:customTextToDisplayAtTime:)]) {
NSTimeInterval atTime = (_timerType == MZTimerLabelTypeStopWatch) ? timeDiff : ((timeUserValue - timeDiff) < 0 ? 0 : (timeUserValue - timeDiff));
NSString *customtext = [_delegate timerLabel:self customTextToDisplayAtTime:atTime];
Expand All @@ -352,20 +347,10 @@ -(void)updateLabel{
}else{
self.timeLabel.text = [self.dateFormatter stringFromDate:timeToShow];
}
}else if([_delegate respondsToSelector:@selector(timerLabel:customAttributedTextToDisplayAtTime:)]){
NSTimeInterval atTime = (_timerType == MZTimerLabelTypeStopWatch) ? timeDiff : ((timeUserValue - timeDiff) < 0 ? 0 : (timeUserValue - timeDiff));
NSAttributedString *customtext = [_delegate timerLabel:self customAttributedTextToDisplayAtTime:atTime];
if ([customtext length]) {
self.timeLabel.attributedText = customtext;
}else{
self.timeLabel.text = [self.dateFormatter stringFromDate:timeToShow];
}

}else{



if(_shouldCountBeyondHHLimit) {
//0.4.7 added---start//
//0.4.7 added---start//
NSString *originalTimeFormat = _timeFormat;
NSString *beyondFormat = [_timeFormat stringByReplacingOccurrencesOfString:@"HH" withString:kHourFormatReplace];
beyondFormat = [beyondFormat stringByReplacingOccurrencesOfString:@"H" withString:kHourFormatReplace];
Expand All @@ -377,7 +362,7 @@ -(void)updateLabel{

self.timeLabel.text = beyondedDate;
self.dateFormatter.dateFormat = originalTimeFormat;
//0.4.7 added---endb//
//0.4.7 added---endb//
}else{
if(self.textRange.length > 0){
if(self.attributedDictionaryForTextInRange){
Expand All @@ -388,7 +373,7 @@ -(void)updateLabel{
attributedString = [[NSMutableAttributedString alloc]initWithString:self.text];
[attributedString replaceCharactersInRange:self.textRange withAttributedString:attrTextInRange];
self.timeLabel.attributedText = attributedString;

} else {
NSString *labelText = [self.text stringByReplacingCharactersInRange:self.textRange withString:[self.dateFormatter stringFromDate:timeToShow]];
self.timeLabel.text = labelText;
Expand All @@ -399,7 +384,7 @@ -(void)updateLabel{
}
}

//0.5.1 moved below to the bottom
//0.5.1 moved below to the bottom
if(timerEnded) {
if([_delegate respondsToSelector:@selector(timerLabel:finshedCountDownTimerWithTime:)]){
[_delegate timerLabel:self finshedCountDownTimerWithTime:timeUserValue];
Expand All @@ -418,4 +403,9 @@ -(void)updateLabel{

}

-(void)updateLabelWihDate:(NSDate*)startedDate{
startCountDate = startedDate;
[self updateLabel];
[self start];
}
@end
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ MZTimerLabel
![Platforms](https://cocoapod-badges.herokuapp.com/p/MZTimerLabel/badge.png)
[![Cocoapod Latest Version](http://img.shields.io/cocoapods/v/MZTimerLabel.svg?style=flat)](https://cocoapods.org/?q=MZTimerLabel)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Downloads with CocoaPods](https://img.shields.io/cocoapods/dt/MZTimerLabel.svg)](https://cocoapods.org/pods/MZTimerLabel)


<img align="center" src="https://raw.github.com/mineschan/MZTimerLabel/master/demo.gif" alt="ScreenShot" width="300">
<img align="center" src="https://raw.github.com/mineschan/MZTimerLabel/master/MZTimerLabel_Demo2.png" alt="ScreenShot2" width="300">
Expand Down