• Stars
    star
    220
  • Rank 180,422 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

👻 A UILabel subclass that adds a typewriting animation effect

Build Status Swift License Platform Twitter

A UILabel subclass that adds a typewriting animation effect - as if a 👻 was typing it directly on your user's device!

GhostTypewriter was inspired by the following post here.

Installation

CocoaPods

To integrate GhostTypewriter into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'

pod 'GhostTypewriter'

Then, run the following command:

$ pod install

CocoaPods 1.1.1+ is required to build GhostTypewriter.

Swift Package Manager

Swift Package Manager is a dependency manager built into Xcode.

If you are using Xcode 11 or higher, go to File -> Add Packages... and enter package repository URL https://github.com/wibosco/GhostTypewriter into the search bar, then follow the instructions.

Usage

Animated Typing

TypewriterLabel is a subclass of UILabel and where the animation (magic) happens. It works by taking advantage of the attributedText property on the label and changing the properties of the text content to gradually expose the text using an animation similar to what you get on a mechanical typewriter.

A TypewriterLabel instance when added as a subview will hide it's content.

Starting

Starting the animation will cause the content of the label to be reveal one character at a time.

How quickly each character is revealed is controlled by setting the typingTimeInterval property.

There are two ways to start an animation: with and without a completion closure.

With a completion closure:

import GhostTypewriter

@IBAction func startAnimationButtonPressed(_ sender: Any) {
    titleLabel.startTypewritingAnimation {
        //Implement your completion closure body here...
    }
}

Without a completion closure:

import GhostTypewriter

@IBAction func startAnimationButtonPressed(_ sender: Any) {
    titleLabel.startTypewritingAnimation()
}

Stopping

Stopping an animation causes the characters that have been revealed to remain as is and no new characters being revealed.

import GhostTypewriter

@IBAction func stopAnimationButtonPressed(_ sender: Any) {
    titleLabel.stopTypewritingAnimation()
}

Resetting

Resetting an animation causes all characters to be hidden.

import GhostTypewriter

@IBAction func resetAnimationButtonPressed(_ sender: Any) {
    titleLabel.resetTypewritingAnimation()
}

It's important to note that resetting an TypewriterLabel instance does not cause the animation to restart instead you need to call restartTypewritingAnimation().

Restarting

Restarting an animation causes all characters to be hidden and for the animation to begin from the start again.

There are two ways to start an animation: with and without a completion closure.

Without a completion closure:

import GhostTypewriter

@IBAction func restartAnimationButtonPressed(_ sender: Any) {
    titleLabel.restartTypewritingAnimation()
}

With a completion closure:

import GhostTypewriter

@IBAction func restartAnimationButtonPressed(_ sender: Any) {
    titleLabel.restartTypewritingAnimation {
        //Implement your completion closure body here...
    }
}

Completing

Completing an animation causes all characters to instantly be revealed.

import GhostTypewriter

@IBAction func completeAnimationButtonPressed(_ sender: Any) {
    titleLabel.completeTypewritingAnimation()
}

Animation Options

Style

By default TypewriterLabel reveals the content as it animates however this can be changed to hiding the content by setting the animationStyle property to .hide:

import GhostTypewriter

override func viewDidLoad() {
    super.viewDidLoad()

    titleLabel.animationStyle = .hide
}

animationStyle is defaulted to .reveal

Direction

By default TypewriterLabel animates from character index 0 to n-1 however this can be changed to go from charcter index n-1 to 0 by setting the animationDirection to .backward:

import GhostTypewriter

override func viewDidLoad() {
    super.viewDidLoad()

    titleLabel.animationDirection = .backward
}

animationDirection is defaulted to .forward.

Adjusting Animation Timing

Each character of a TypewriterLabel instance is revealed at a pace set by the typingTimeInterval property.

typingTimeInterval defaults to 0.1 second.

import GhostTypewriter

override func viewDidLoad() {
    super.viewDidLoad()

    titleLabel.typingTimeInterval = 0.3
}

It's important to note that setting/changing typingTimeInterval after an animation has been started, has no affect on the timing of that animation.

Storyboards

As TypewriterLabel is contained in a pod, when using it with storyboards you will need to set the Module field to GhostTypewriter.

Migrating from v1 to v2

Version 2.0.0 of GhostTypewriter contains breaking changes.

  • cancelTypewritingAnimation() now use resetTypewritingAnimation().
  • cancelTypewritingAnimation(clearText: true) now use resetTypewritingAnimation().
  • cancelTypewritingAnimation(clearText: false) now use stopTypewritingAnimation().

Example

GhostTypewriter comes with an example project to provide more details than listed above.

Found an issue?

Please open a new Issue here if you run into a problem specific to GhostTypewriterLabel, have a feature request, or want to share a comment.

Pull requests are encouraged and greatly appreciated! Please try to maintain consistency with the existing code style. If you're considering taking on significant changes or additions to the project, please communicate in advance by opening a new Issue. This allows everyone to get onboard with upcoming changes, ensures that changes align with the project's design philosophy, and avoids duplicated work.

More Repositories

1

CoreDataMigrationRevised-Example

An example project showing how to implement progressive Core Data migrations
Swift
64
star
2

NetworkingInOperations-Example

An example project looking at how to combine Operations with URLSession 📡
Swift
40
star
3

NetworkingWingman-Example

Example of combining NSURLSession with NSOperation to form one task
Objective-C
30
star
4

BackgroundTransfer-Example

An example project looking at how to implement background transfers on iOS
Swift
23
star
5

SSDPDiscovery-Example

An example project written in Swift on how to use SSDP to discover services using BlueSocket 🔭
Swift
21
star
6

CoreDataMigration-Example

An example project showing how to implement data migration using NSPersistentContainer
Swift
18
star
7

CodingChallenges

A collection of coding challenges from LeetCode, HackerRank and CtCI
Swift
17
star
8

ViewControllerInCell-Example

An example project looking at how to add a view controller's view as a subview in a cell
Swift
14
star
9

ConvenientFileManager

A suite of categories to ease using (NS)FileManager for common tasks.
Swift
12
star
10

CoreDataServices

CoreDataServices is a suite of helper classes and extensions to help to remove some of the boilerplate that surrounds using Core Data.
Swift
11
star
11

SwiftPaginationCoreData-Example

Swift pagination core data example
Swift
8
star
12

CustomAlert-Example

An example project written in Swift on how to build and present custom alerts
Swift
6
star
13

TestingWithCoreData-Example

An example project looking at how we can use Core Data in our unit tests
Swift
6
star
14

GhostTyping-Example

An example project to show how we can implement a character-by-character animation
Swift
6
star
15

AlertQueue-Example

An example project written in Swift on how to build an alert queue
Swift
6
star
16

ImmutableDateFormatters-Example

An example project looking at how to avoid treating DateFormatters as mutable singletons 📆
Swift
6
star
17

InsightfulPager

InsightfulPager is a more chatty alternative to UIPageViewController
Objective-C
4
star
18

FetchedResultsController

A FetchedResultsController implementation that abstracts out the boilerplate for both UITableView and UICollectionView
Swift
4
star
19

PausableDownloads-Example

An example project about pausing and resuming download requests
Swift
4
star
20

CoalescingOperationsReducedBoilerplate-Example

An example project to show how it's possible to coalesce NSOperation subclasses's callbacks with reduced boilerplate compared to previous examples
Swift
3
star
21

R.swift-Example

A Swift example on how to use R.swift to destring your project
Swift
2
star
22

AnalyticalAppetite-Example

An example project to show how we can implement analytical event tracking with minimal impact and maximum test coverage
Swift
2
star
23

SSDPDiscoveryWithNetworkFramework-Example

An example project written in Swift on how to use SSDP to discover services using the Network framework from Apple 🔭
Swift
2
star
24

MakingRequests-Example

An example project to show how using custom request classes can lead to a more modular project that is easier to test
Swift
2
star
25

LottieCircleProgressView-Example

An example project looking at using Lottie with a data-driven progress animation
Ruby
2
star
26

AvoidingModal-Example

An example project to show how for on-boarding sections of our apps we can avoid using modal viewcontrllers as "walls"
Swift
2
star
27

ModelingFormChanges-Example

An example project to show how we can implement a model to simplify form validation
Swift
2
star
28

DispatchGroup-Example

An example project written in Swift on how to use dispatch groups
Swift
2
star
29

ResponderNavigation-Example

A look at how we can use the responder chain to navigate between modules
Swift
2
star
30

CardTransitionWithViewControllers-Example

An example project looking at producing a view transition similar to the App Store card transition by using child view controllers
Swift
2
star
31

PaginationCoreData-Example

Pagination core data example
Objective-C
1
star
32

Networking-Example

A Swift example project on how to build a networking module using async/await
Swift
1
star
33

SwiftGen-Example

A Swift example on how to use SwiftGen to destring your project
Swift
1
star
34

CoincidentalDuplication-Example

A project showing an example coincidental duplication
Swift
1
star
35

PaginationThrowAway-Example

Pagination throw away example
Objective-C
1
star
36

StreamingContent-Example

An example project on how to stream content
Objective-C
1
star
37

KVOWithSwift-Example

Playground looking at how to use KVO with Swift
Swift
1
star
38

AmbiguousDecoding-Example

A Swift example project on how Encodable and Decodable can overcome inconsistencies in the type systems between Swift and JSON
Swift
1
star
39

UnleashingBuildSettings-Example

How to use build configuration to control environment variables
Swift
1
star
40

CircleProgressView-Example

An example project looking at different ways to build a circle progress view
Swift
1
star
41

AvoidingFixedStringDateFormats-Example

An example project looking at how we can avoid using the `dateFormat` property on DateFormatters to provide the user with a date value that they would actually recognise.
Swift
1
star
42

CapturingTheCallbackQueue-Example

An example project showing how we can capture the callback queue across threads
Swift
1
star