-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLTypingLabel.swift
182 lines (149 loc) · 5.84 KB
/
CLTypingLabel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// CLTypingLabel.swift
// CLTypingLabel
// The MIT License (MIT)
// Copyright © 2016 Chenglin 2/21/16.
//
// 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
/*
Set text at runtime to trigger type animation;
Set charInterval property for interval time between each character, default is 0.1;
Call pauseTyping() to pause animation;
Call continueTyping() to restart a paused animation;
*/
@IBDesignable open class CLTypingLabel: UILabel {
/*
Set interval time between each characters
*/
@IBInspectable open var charInterval: Double = 0.1
/*
Optional handler which fires when typing animation is finished
*/
open var onTypingAnimationFinished: (() -> Void)?
/*
If text is always centered during typing
*/
@IBInspectable open var centerText: Bool = true
private var typingStopped: Bool = false
private var typingOver: Bool = true
private var stoppedSubstring: String?
private var attributes: [NSAttributedString.Key: Any]?
private var currentDispatchID: Int = 320
private let dispatchSerialQ = DispatchQueue(label: "CLTypingLableQueue")
/*
Setting the text will trigger animation automatically
*/
override open var text: String! {
get {
return super.text
}
set {
if charInterval < 0 {
charInterval = -charInterval
}
currentDispatchID += 1
typingStopped = false
typingOver = false
stoppedSubstring = nil
attributes = nil
setTextWithTypingAnimation(newValue, attributes,charInterval, true, currentDispatchID)
}
}
/*
Setting attributed text will trigger animation automatically
*/
override open var attributedText: NSAttributedString! {
get {
return super.attributedText
}
set {
if charInterval < 0 {
charInterval = -charInterval
}
currentDispatchID += 1
typingStopped = false
typingOver = false
stoppedSubstring = nil
attributes = newValue.attributes(at: 0, effectiveRange: nil)
setTextWithTypingAnimation(newValue.string, attributes,charInterval, true, currentDispatchID)
}
}
// MARK: -
// MARK: Stop Typing Animation
open func pauseTyping() {
if typingOver == false {
typingStopped = true
}
}
// MARK: -
// MARK: Continue Typing Animation
open func continueTyping() {
guard typingOver == false else {
print("CLTypingLabel: Animation is already over")
return
}
guard typingStopped == true else {
print("CLTypingLabel: Animation is not stopped")
return
}
guard let stoppedSubstring = stoppedSubstring else {
return
}
typingStopped = false
setTextWithTypingAnimation(stoppedSubstring, attributes ,charInterval, false, currentDispatchID)
}
// MARK: -
// MARK: Set Text Typing Recursive Loop
private func setTextWithTypingAnimation(_ typedText: String, _ attributes: Dictionary<NSAttributedString.Key, Any>?, _ charInterval: TimeInterval, _ initial: Bool, _ dispatchID: Int) {
guard !typedText.isEmpty && currentDispatchID == dispatchID else {
typingOver = true
typingStopped = false
if let nonNilBlock = onTypingAnimationFinished {
DispatchQueue.main.async(execute: nonNilBlock)
}
return
}
guard typingStopped == false else {
stoppedSubstring = typedText
return
}
if initial == true {
super.text = ""
}
let firstCharIndex = typedText.index(typedText.startIndex, offsetBy: 1)
DispatchQueue.main.async {
if let attributes = attributes {
super.attributedText = NSAttributedString(string: super.attributedText!.string + String(typedText[..<firstCharIndex]),
attributes: attributes)
} else {
super.text = super.text! + String(typedText[..<firstCharIndex])
}
if self.centerText == true {
self.sizeToFit()
}
self.dispatchSerialQ.asyncAfter(deadline: .now() + charInterval) { [weak self] in
let nextString = String(typedText[firstCharIndex...])
self?.setTextWithTypingAnimation(nextString, attributes, charInterval, false, dispatchID)
}
}
}
}