A handy class for iOS to use UILabel as a countdown timer or stopwatch just like in Apple Clock App.

Related tags

Label MZTimerLabel
Overview

MZTimerLabel

License Platforms Cocoapod Latest Version Carthage compatible Downloads with CocoaPods

ScreenShot

ScreenShot2

Purpose

MZTimerLabel is a UILabel subclass, which is a handy way to use UILabel as a countdown timer or stopwatch just like that in Apple Clock App with just 2 lines of code. MZTimerLabel also provides delegate method for you to define the action when the timer finished.

Author: MineS Chan and awesome contributors.

Remark: This is my first iOS plugin project on github, please accept my apologize if any bad coding.

Requirements

  • ARC
  • iOS 5.0+

Installations

Manual

  1. Download or clone MZTimerLabel, add MZTimerLabel.h and MZTimerLabel.m source files into your project.
  2. #import "MZTimerLabel.h" whereever you need it.

CocoaPods

(Unfamiliar with CocoaPods yet? It's a dependency management tool for iOS and Mac, check it out!)

pod 'MZTimerLabel'

Carthage

Another dependency manager is Carthage, which does not have a centralized repository.

github "mineschan/MZTimerLabel"

Easy Example

To use MZTimerLabel as a stopwatch and counter, you need only 2 lines.

   MZTimerLabel *stopwatch = [[MZTimerLabel alloc] initWithLabel:aUILabel];
   [stopwatch start];

Easy? If you are looking for a timer, things is just similar.

   MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:aUILabel andTimerType:MZTimerLabelTypeTimer];
   [timer setCountDownTime:60];
   [timer start];

Now the timer will start counting from 60 to 0 ;)

Custom Appearance

As MZTimerLabel is a UILabel subclass, you can directly allocate it as a normal UILabel and customize timeLabel property just like usual.

   MZTimerLabel *redStopwatch = [[MZTimerLabel alloc] init];
   redStopwatch.frame = CGRectMake(100,50,100,20);
   redStopwatch.timeLabel.font = [UIFont systemFontOfSize:20.0f];
   redStopwatch.timeLabel.textColor = [UIColor redColor];
   [self.view addSubview:redStopwatch];
   [redStopwatch start];

MZTimerLabel uses 00:00:00 (HH:mm:ss) as time format, if you prefer using another format such as including milliseconds.Your can set your time format like below.

timerExample4.timeFormat = @"HH:mm:ss SS";

Control the timer

You can start,pause,reset your timer with your custom control, set your control up and call these methods:

-(void)start;
-(void)pause;
-(void)reset;

Getter and Setters

You may control the time value and behaviours at the begining or during runtime with these properties and methods

@property (assign) BOOL shouldCountBeyondHHLimit;   //see example #12
@property (assign) BOOL resetTimerAfterFinish;      //see example #7

-(void)setCountDownTime:(NSTimeInterval)time;
-(void)setStopWatchTime:(NSTimeInterval)time;
-(void)setCountDownToDate:(NSDate*)date;
-(void)addTimeCountedByTime:(NSTimeInterval)timeToAdd; //see example #10, #11

And if you want to have information of the timer, here is how.

@property (assign,readonly) BOOL counting;  //see example #4-7

- (NSTimeInterval)getTimeCounted;    //see example #3
- (NSTimeInterval)getTimeRemaining;  //see example #3
- (NSTimeInterval)getCountDownTime;  

Timer Finish Handling

Usually when you need a timer, you need to deal with it after it finished. Following are 2 examples showing how to do it using delegate and block methods.

Delegate

First, set the delegate of the timer label.

timer.delegate = self;

And then implement MZTimerLabelDelegate protocol in your dedicated class

@interface ViewController : UIViewController<MZTimerLabelDelegate>

Finally, implement the delegate method timerLabel:finshedCountDownTimerWithTimeWithTime:

-(void)timerLabel:(MZTimerLabel*)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime{
   //time is up, what should I do master?
}

Blocks

Block is a very convenient way to handle the callbacks, MZTimerLabel makes your life even easier.

   MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:aUILabel andTimerType:MZTimerLabelTypeTimer];
   [timer3 setCountDownTime:60]; 
   [timer startWithEndingBlock:^(NSTimeInterval countTime) {
       //oh my gosh, it's awesome!!
   }];

Or set it seperately

   [timer3 setCountDownTime:60]; 
   timer.endedBlock = ^(NSTimeInterval countTime) {
       //oh my gosh, it's awesome!!
   };
   [timer start];

More Examples

Please check the demo project I provided, with well explained example code inside.

License

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

What's coming up next?

  1. Submit to CocaPods
  2. Better performance.
  3. Your suggestions!:D
Comments
  • Bug in countdown timer, If count down time is more than 24 hours away.

    Bug in countdown timer, If count down time is more than 24 hours away.

    I experience there is a bug in countdown timer, if the count down date time is more than 24 hours away from current time. For example if you pass, [timerLabel setCountDownTime:3600*26] . It will start counting down for 2 hours.

    opened by warunacds 8
  • MZTimerLabel in a multiview application

    MZTimerLabel in a multiview application

    Hi! Thanks for the pod! I'm currently having issues in a multiview application. I've a stopwatch running in a view A but if I change to view B and I return to A the stopwatch is stop. How can I fix this behaviour ?

    opened by smashkins 6
  • Timer Counting Up

    Timer Counting Up

    I create the timer and call... self.countdown.setCountDownTime(60) self.countdown.start()

    but the timer label starts at 1 minute then starts to count up. Any help?

    opened by rsickles 5
  • Adding a block for the timer independently of starting it

    Adding a block for the timer independently of starting it

    Hi and thanks for your great classes !

    I was wondering : could you add a method to add a block to execute when a countDown timer ends independently of starting it ? I mean we could do

    timerLabel.endingBlock = ^(NSTimeIntervall time){...} ;
    

    Thanks

    opened by colasbd 5
  • customTextToDisplayAtTime called way too frequently

    customTextToDisplayAtTime called way too frequently

    customTextToDisplayAtTime is getting called every 10 milliseconds or so in my app. Seems way to frequent for a timer where the smallest time I'm showing is seconds. Is there any way to change this?

    opened by holgersindbaek 4
  • Timer stops too early

    Timer stops too early

    //Creating a timer _timerView = [[MZTimerLabel alloc] initWithLabel:_timerLabel andTimerType:MZTimerLabelTypeTimer]; _timerView.timeFormat = @"HH:mm:ss,SS"; _timerView.countDownTime = _currentTimer.timeInterval; // timeInterval is for example 14 _timerView.delegate = self; [_timerView start];

    timerLabel:(MZTimerLabel *)timerLabel finshedCountDownTimerWithTime:(NSTimeInterval)countTime is called when the label shows 00:00:00,99 so it doesn't count to zero.

    opened by ewalddieser 4
  • MZTimerLabel, pause/start method not working properly

    MZTimerLabel, pause/start method not working properly

    First of all thanks for this great pod/control

    I have encountered an issue and would like to inform you. So I have MZTimerLabel in my view, actually it is a normal UILabel in storyboard file but in the code I used the initWithLabel: method to create a MZTimerLabel with an existing UILabel, everything works fine.

    But then I wanted to include a pause screen in my viewController in my pauseTimer; function I called [timerLabel pause]; and did some other work. but when I resume the timer in another function of mine by calling [timerLabel start]; it would not resume from that time, rather it would resume from some random time which is 3-15 seconds added to the current time or just go to zero by its own.

    I am waiting for your reply. Thanks

    opened by RayanSaeed 4
  • Countdown not working for more than 24 hours

    Countdown not working for more than 24 hours

    Its not working in this scenario, please assist.

    MZTimerLabel *timer = [[MZTimerLabel alloc] initWithLabel:someLabel andTimerType:MZTimerLabelTypeTimer]; [timer setShouldCountBeyondHHLimit:YES]; [timer setCountDownTime:3600 * 50]; [timer start];

    opened by SMFakhir 3
  • Zombie, memory leaks

    Zombie, memory leaks

    When you create instance of MZTimerLabel and it is not added as subview (You just create instance and then set timeLabel), removeFromSuperview is not fired > timer is not invalidated and instance stays in memory.

    opened by sirljan 3
  • MZTimerLabelTypeTimer countdown endBlock is called 1 second before actually.

    MZTimerLabelTypeTimer countdown endBlock is called 1 second before actually.

    Hi, great job!!

    But, there is an issue, countdown time end block is not called properly, actually it is called 1 second before the finish time. Here is my code:

    MZTimerLabel *countDownTimer = [[MZTimerLabel alloc] initWithLabel:secondsLabel andTimerType:MZTimerLabelTypeTimer];
    [countDownTimer setCountDownTime:15];
    countDownTimer.resetTimerAfterFinish = YES;
    countDownTimer.timeFormat = @"mm:ss SS";
    [countDownTimer startWithEndingBlock:^(NSTimeInterval countTime) {
          // Called when 14 seconds passed
    }];
    
    opened by mixdesign 3
  • Edit Timer

    Edit Timer

    Hello,

    What is the best way to edit the timer.

    For example we have a timer running and then a user can tap and edit the current time displayed. I edit the label and start but it starts from the previous time.

    I have tried "setStopWatch" but it only accepts NSTimeInterval, is their a quick way to edit the timer?

    opened by adamrushy 3
  • if formatted to just show seconds does not adjust for not having a 'minutes' place

    if formatted to just show seconds does not adjust for not having a 'minutes' place

    If you want the countdown timer to run for say 90 seconds but you want it to go 90...89...88...etc it will not do it because it assumes there is a minute digit displayed there. It's not a big deal but figured I'd report it anyway. Maybe I'll put a pull request in if I decide I really need it.

    It's for a game so that's why i want it to be just seconds. And we all know there is nothing fun about a colon! I really like the pod though. Nice work.

    opened by nickthedude 0
Releases(0.5.5)
Owner
Mines Chan
Mines Chan
Glitching UILabel for iOS 📺

Glitching UILabel for iOS ?? Created by Lee Sun-Hyoup. Try the sample $ pod try GlitchLabel Requirements iOS 8.0+ Swift 3 Xcode 8 Installation CocoaP

Lee Sun-Hyoup 1k Dec 14, 2022
Incrementable UILabel for iOS and tvOS

IncrementableLabel IncrementableLabel is the easiest way to have incrementable numbers in an UILabel! Usage let myIncrementableLabel = IncrementableLa

Tom Baranes 81 Feb 4, 2022
UILabel drop-in replacement supporting Hashtags

ActiveLabel.swift UILabel drop-in replacement supporting Hashtags (#), Mentions (@), URLs (http://), Emails and custom regex patterns, written in Swif

Optonaut 4.2k Dec 31, 2022
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.

Atributika is an easy and painless way to build NSAttributedString. It is able to detect HTML-like tags, links, phone numbers, hashtags, any regex or

Pavel Sharanda 1.1k Dec 26, 2022
KDEDateLabel is an UILabel subclass that updates itself to make time ago's format easier.

KDEDateLabel KDEDateLabel is an UILabel subclass that saves you from refreshing it when using 'time ago' format. Installation You have multiple choice

Kevin Delannoy 117 May 16, 2022
A morphing UILabel subclass written in Swift.

LTMorphingLabel A morphing UILabel subclass written in Swift. The .Scale effect mimicked Apple's QuickType animation of iOS 8 of WWDC 2014. New morphi

Lex Tang 7.9k Jan 4, 2023
UILabel with image placed from left or right

SMIconLabel UILabel with possibility to place small icon on the left or on the right side. Take a look at preview image or build sample app to see how

Anatoliy Voropay 94 Apr 27, 2022
A drop-in replacement for UILabel that supports attributes, data detectors, links, and more

TTTAttributedLabel A drop-in replacement for UILabel that supports attributes, data detectors, links, and more TTTAttributedLabel is a drop-in replace

TTTAttributedLabel 8.8k Dec 30, 2022
UILabel subclass, which additionally allows shadow blur, inner shadow, stroke text and fill gradient.

THLabel THLabel is a subclass of UILabel, which additionally allows shadow blur, inner shadow, stroke text and fill gradient. Requirements iOS 4.0 or

Tobias Hagemann 654 Nov 27, 2022
UILabel replacement with fine-grain appear/disappear animation

ZCAnimatedLabel UILabel-like view with easy to extend appear/disappear animation Features Rich text support (with NSAttributedString) Group aniamtion

Chen 2.3k Dec 5, 2022
MTLLinkLabel is linkable UILabel. Written in Swift.

# MTLLinkLabel is linkable UILabel. Written in Swift. Requirements iOS 8.0+ Xcode 7.3+ Installation Carthage You can install Carthage with Homebrew. $

Recruit Holdings. Media Technology Lab 74 Jul 1, 2022
Adds animated counting support to UILabel.

UICountingLabel Adds animated counting support to UILabel. CocoaPods UICountingLabel is available on CocoaPods. Add this to your Podfile: pod 'UICount

Tim Gostony 1.9k Dec 1, 2022
A simple designable subclass on UILabel with extra IBDesignable and Blinking features.

JSLabel Demo pod try JSLabel ...or clone this repo and build and run/test the JSLabel project in Xcode to see JSLabel in action. If you don't have Coc

Jogendra 6 May 9, 2022
Secret app like text animation

RQShineLabel A UILabel subclass that lets you animate text similar to Secret app. Installation CocoaPods RQShineLabel is available through CocoaPods,

gk 2k Dec 6, 2022
NumberMorphView a view like label for displaying numbers which animate with transition using a technique called number tweening or number morphing.

NumberMorphView a view like label for displaying numbers which animate with transition using a technique called number tweening or num

Abhinav Chauhan 1.6k Dec 21, 2022
A triangle shaped corner label view for iOS written in Swift.

A triangle shaped corner label view for iOS written in Swift. This view is a subclass of UIView. It can be created and customized from the Storyboard

Mukesh Thawani 167 Nov 30, 2022
A faster and more flexible label view for iOS

STULabel is an open source iOS framework for Swift and Objective-C that provides a label view (STULabel), a label layer (STULabelLayer) and a flexible

Stephan Tolksdorf 96 Dec 22, 2022
A basic countdown app that allows the user to create, edit, and delete events. Each event contains a live countdown timer to a specified date and time.

Event Countdown App (iOS) Created by Lucas Ausberger About This Project Created: January 4, 2021 Last Updated: January 8, 2021 Current Verison: 1.1.1

Lucas Ausberger 1 Jan 8, 2022
AsyncTimer is a precision asynchronous timer. You can also use it as a countdown timer

AsyncTimer ?? Features Can work as a countdown timer Can work as a periodic Timer Can work as a scheduled timer Working with user events (like: scroll

Adrian Bobrowski 26 Jan 1, 2023
SwiftUI library to display a clock. You can move the arms to change the time, change the style of the clock and customise some configurations.

SwiftClockUI Clock UI for SwiftUI This library has been tested ✅ ?? macOS Catalina 10.15.3 ✅ ?? macOS Big Sur 11.6 ✅ ?? iOS 13 ✅ ?? iOS 14 ✅ ?? iOS 15

Renaud Jenny 239 Dec 29, 2022