8
8
import Foundation
9
9
import UIKit
10
10
11
+ /// Font size of the toast
12
+
11
13
/// Supporting UILabel for Toast.
14
+ /// Setups the base label.
15
+ /// The toast adds the position color
12
16
class ToastLabel : UILabel {
17
+
18
+ static let kFontSize = 16
19
+
20
+ override init ( frame: CGRect ) {
21
+ super. init ( frame: frame)
22
+ commonInit ( )
23
+ }
24
+
25
+ required init ? ( coder aDecoder: NSCoder ) {
26
+ super. init ( coder: aDecoder)
27
+ commonInit ( )
28
+ }
29
+
30
+ convenience init ( text: String ) {
31
+ self . init ( )
32
+ self . text = text
33
+ }
34
+
35
+ private func commonInit( ) {
36
+ textAlignment = . center
37
+ font = UIFont . systemFont ( ofSize: CGFloat ( ToastLabel . kFontSize) )
38
+ alpha = 0
39
+ numberOfLines = 0
40
+ layer. masksToBounds = true
41
+ layer. cornerRadius = 10
42
+ }
43
+
13
44
var textInsets = UIEdgeInsets . zero {
14
45
didSet { invalidateIntrinsicContentSize ( ) }
15
46
}
@@ -56,9 +87,6 @@ class Toast {
56
87
/// Offset from the closest screen edge (top or bottom).
57
88
static let kToastOffset = 120
58
89
59
- /// Font size of the toast
60
- static let kFontSize = 16
61
-
62
90
/// Toast.regular text color
63
91
static let kRegularTextColor : UIColor = UIColor . white
64
92
/// Toast.regular background color
@@ -91,7 +119,6 @@ class Toast {
91
119
case top
92
120
}
93
121
94
-
95
122
///
96
123
/// Generic implementation to show toast
97
124
/// - Parameters:
@@ -100,18 +127,18 @@ class Toast {
100
127
/// - backgroundColor: Color of the text
101
128
/// - position: Position within the screen (.bottom, .center, .top)
102
129
/// - delay: time in seconds that the toast will be displayed
103
- static func showToast( _ message: String , textColor: UIColor , backgroundColor: UIColor , position: Position , delay: Double ) {
130
+ static func showToast( _ message: String ,
131
+ textColor: UIColor = kRegularTextColor,
132
+ backgroundColor: UIColor = kRegularBackgroundColor,
133
+ position: Position = . bottom,
134
+ delay: Double = kDelayLong) {
104
135
guard let window = UIApplication . shared. keyWindow else {
105
136
return
106
137
}
107
- let label = ToastLabel ( )
138
+ let label = ToastLabel ( text : message )
108
139
label. textColor = textColor
109
140
label. backgroundColor = backgroundColor
110
- label. textAlignment = . center
111
- label. font = UIFont . systemFont ( ofSize: CGFloat ( kFontSize) )
112
- label. alpha = 0
113
- label. text = message
114
- label. numberOfLines = 0
141
+
115
142
var vertical : CGFloat = 0
116
143
var size = label. intrinsicContentSize
117
144
var width = min ( size. width, window. frame. width - 60 )
@@ -120,32 +147,38 @@ class Toast {
120
147
label. textAlignment = . justified
121
148
}
122
149
label. textInsets = UIEdgeInsets ( top: vertical, left: 15 , bottom: vertical, right: 15 )
150
+
123
151
size = label. intrinsicContentSize
124
152
width = min ( size. width, window. frame. width - 60 )
125
-
126
- if position == Position . bottom {
153
+ switch position {
154
+ case . bottom:
127
155
label. frame = CGRect ( x: CGFloat ( kToastWidth) ,
128
156
y: window. frame. height - CGFloat( kToastOffset) ,
129
157
width: width,
130
158
height: size. height + CGFloat( kToastHeight) )
131
- } else if position == Position . center {
132
- label. frame = CGRect ( x: CGFloat ( kToastWidth) , y: window. frame. height / 2 , width: width, height: size. height + CGFloat( kToastHeight) )
133
- } else if position == Position . top {
134
- label. frame = CGRect ( x: CGFloat ( kToastWidth) , y: CGFloat ( kToastOffset) , width: width, height: size. height + CGFloat( kToastHeight) )
159
+ case . center:
160
+ label. frame = CGRect ( x: CGFloat ( kToastWidth) ,
161
+ y: window. frame. height / 2 ,
162
+ width: width,
163
+ height: size. height + CGFloat( kToastHeight) )
164
+ case . top:
165
+ label. frame = CGRect ( x: CGFloat ( kToastWidth) ,
166
+ y: CGFloat ( kToastOffset) ,
167
+ width: width,
168
+ height: size. height + CGFloat( kToastHeight) )
135
169
}
136
170
label. center. x = window. center. x
137
- label. layer. cornerRadius = min ( label. frame. height / 2 , 32 )
138
- label. layer. masksToBounds = true
171
+
139
172
window. addSubview ( label)
140
- UIView . animate ( withDuration: 0.5 , delay: 0 , options: . curveEaseIn, animations : {
141
- label. alpha = 1
142
- } , completion: { _ in
143
- UIView . animate ( withDuration: 0.5 , delay: delay, options : . curveEaseOut , animations : {
144
- label . alpha = 0
145
- } , completion : { _ in
146
- label. removeFromSuperview ( )
147
- } )
148
- } )
173
+ UIView . animate ( withDuration: 0.5 , delay: 0 , options: . curveEaseIn,
174
+ animations : { label. alpha = 1 } ,
175
+ completion: { _ in
176
+ UIView . animate ( withDuration: 0.5 , delay: delay,
177
+ options : . curveEaseOut ,
178
+ animations : { label . alpha = 0 } ,
179
+ completion : { _ in label. removeFromSuperview ( )
180
+ } )
181
+ } )
149
182
}
150
183
151
184
///
@@ -160,35 +193,59 @@ class Toast {
160
193
/// Information toast (blue)
161
194
///
162
195
/// - SeeAlso showToast
163
- class func info( _ message: String , position: Position = Position . bottom, delay: Double = kDelayLong) {
164
- showToast ( String ( " \u{24D8} " ) + " " + message, textColor: kInfoTextColor, backgroundColor: kInfoBackgroundColor,
165
- position: position, delay: delay)
196
+ class func info( _ message: String ,
197
+ position: Position = Position . bottom,
198
+ delay: Double = kDelayLong) {
199
+
200
+ showToast ( String ( " \u{24D8} " ) + " " + message,
201
+ textColor: kInfoTextColor,
202
+ backgroundColor: kInfoBackgroundColor,
203
+ position: position,
204
+ delay: delay)
166
205
}
167
206
168
207
///
169
208
/// Display a warning toast (orange)
170
209
///
171
210
/// - SeeAlso showToast
172
- class func warning( _ message: String , position: Position = Position . bottom, delay: Double = kDelayLong) {
173
- showToast ( String ( " \u{26A0} " ) + " " + message, textColor: kWarningTextColor, backgroundColor: kWarningBackgroundColor,
174
- position: position, delay: delay)
211
+ class func warning( _ message: String ,
212
+ position: Position = Position . bottom,
213
+ delay: Double = kDelayLong) {
214
+
215
+ showToast ( String ( " \u{26A0} " ) + " " + message,
216
+ textColor: kWarningTextColor,
217
+ backgroundColor: kWarningBackgroundColor,
218
+ position: position,
219
+ delay: delay)
175
220
}
176
221
177
222
///
178
223
/// Display a Success toast
179
224
///
180
225
/// - SeeAlso showToast
181
- class func success( _ message: String , position: Position = Position . bottom, delay: Double = kDelayLong) {
182
- showToast ( String ( " \u{2705} " ) + " " + message, textColor: kSuccessTextColor, backgroundColor: kSuccessBackgroundColor, position: position, delay: delay)
226
+ class func success( _ message: String ,
227
+ position: Position = Position . bottom,
228
+ delay: Double = kDelayLong) {
229
+
230
+ showToast ( String ( " \u{2705} " ) + " " + message,
231
+ textColor: kSuccessTextColor,
232
+ backgroundColor: kSuccessBackgroundColor,
233
+ position: position,
234
+ delay: delay)
183
235
}
184
236
185
237
///
186
- /// Display a error toast
238
+ /// Display a error toast.
187
239
///
188
240
/// - SeeAlso showToast
189
- class func error( _ message: String , position: Position = Position . bottom, delay: Double = kDelayLong) { //2757
190
- showToast ( String ( " \u{274C} " ) + " " + message, textColor: kErrorTextColor, backgroundColor: kErrorBackgroundColor, position: position, delay: delay)
241
+ ///
242
+ class func error( _ message: String ,
243
+ position: Position = Position . bottom,
244
+ delay: Double = kDelayLong) {
245
+ showToast ( String ( " \u{274C} " ) + " " + message,
246
+ textColor: kErrorTextColor,
247
+ backgroundColor: kErrorBackgroundColor,
248
+ position: position,
249
+ delay: delay)
191
250
}
192
-
193
251
}
194
-
0 commit comments