Toast.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. //
  2. // Toast.swift
  3. // Toast-Swift
  4. //
  5. // Copyright (c) 2015-2024 Charles Scalesse.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a
  8. // copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included
  16. // in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  22. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. import UIKit
  26. import ObjectiveC
  27. /**
  28. Toast is a Swift extension that adds toast notifications to the `UIView` object class.
  29. It is intended to be simple, lightweight, and easy to use. Most toast notifications
  30. can be triggered with a single line of code.
  31. The `makeToast` methods create a new view and then display it as toast.
  32. The `showToast` methods display any view as toast.
  33. */
  34. public extension UIView {
  35. /**
  36. Keys used for associated objects.
  37. */
  38. private struct ToastKeys {
  39. static var timer = malloc(1)
  40. static var duration = malloc(1)
  41. static var point = malloc(1)
  42. static var completion = malloc(1)
  43. static var activeToasts = malloc(1)
  44. static var activityView = malloc(1)
  45. static var queue = malloc(1)
  46. }
  47. /**
  48. Swift closures can't be directly associated with objects via the
  49. Objective-C runtime, so the (ugly) solution is to wrap them in a
  50. class that can be used with associated objects.
  51. */
  52. private class ToastCompletionWrapper {
  53. let completion: ((Bool) -> Void)?
  54. init(_ completion: ((Bool) -> Void)?) {
  55. self.completion = completion
  56. }
  57. }
  58. private enum ToastError: Error {
  59. case missingParameters
  60. }
  61. private var activeToasts: NSMutableArray {
  62. get {
  63. if let activeToasts = objc_getAssociatedObject(self, &ToastKeys.activeToasts) as? NSMutableArray {
  64. return activeToasts
  65. } else {
  66. let activeToasts = NSMutableArray()
  67. objc_setAssociatedObject(self, &ToastKeys.activeToasts, activeToasts, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  68. return activeToasts
  69. }
  70. }
  71. }
  72. private var queue: NSMutableArray {
  73. get {
  74. if let queue = objc_getAssociatedObject(self, &ToastKeys.queue) as? NSMutableArray {
  75. return queue
  76. } else {
  77. let queue = NSMutableArray()
  78. objc_setAssociatedObject(self, &ToastKeys.queue, queue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  79. return queue
  80. }
  81. }
  82. }
  83. // MARK: - Make Toast Methods
  84. /**
  85. Creates and presents a new toast view.
  86. @param message The message to be displayed
  87. @param duration The toast duration
  88. @param position The toast's position
  89. @param title The title
  90. @param image The image
  91. @param style The style. The shared style will be used when nil
  92. @param completion The completion closure, executed after the toast view disappears.
  93. didTap will be `true` if the toast view was dismissed from a tap.
  94. */
  95. func makeToast(_ message: String?, duration: TimeInterval = ToastManager.shared.duration, position: ToastPosition = ToastManager.shared.position, title: String? = nil, image: UIImage? = nil, style: ToastStyle = ToastManager.shared.style, completion: ((_ didTap: Bool) -> Void)? = nil) {
  96. do {
  97. let toast = try toastViewForMessage(message, title: title, image: image, style: style)
  98. showToast(toast, duration: duration, position: position, completion: completion)
  99. } catch ToastError.missingParameters {
  100. print("Error: message, title, and image are all nil")
  101. } catch {}
  102. }
  103. /**
  104. Creates a new toast view and presents it at a given center point.
  105. @param message The message to be displayed
  106. @param duration The toast duration
  107. @param point The toast's center point
  108. @param title The title
  109. @param image The image
  110. @param style The style. The shared style will be used when nil
  111. @param completion The completion closure, executed after the toast view disappears.
  112. didTap will be `true` if the toast view was dismissed from a tap.
  113. */
  114. func makeToast(_ message: String?, duration: TimeInterval = ToastManager.shared.duration, point: CGPoint, title: String?, image: UIImage?, style: ToastStyle = ToastManager.shared.style, completion: ((_ didTap: Bool) -> Void)?) {
  115. do {
  116. let toast = try toastViewForMessage(message, title: title, image: image, style: style)
  117. showToast(toast, duration: duration, point: point, completion: completion)
  118. } catch ToastError.missingParameters {
  119. print("Error: message, title, and image cannot all be nil")
  120. } catch {}
  121. }
  122. // MARK: - Show Toast Methods
  123. /**
  124. Displays any view as toast at a provided position and duration. The completion closure
  125. executes when the toast view completes. `didTap` will be `true` if the toast view was
  126. dismissed from a tap.
  127. @param toast The view to be displayed as toast
  128. @param duration The notification duration
  129. @param position The toast's position
  130. @param completion The completion block, executed after the toast view disappears.
  131. didTap will be `true` if the toast view was dismissed from a tap.
  132. */
  133. func showToast(_ toast: UIView, duration: TimeInterval = ToastManager.shared.duration, position: ToastPosition = ToastManager.shared.position, completion: ((_ didTap: Bool) -> Void)? = nil) {
  134. let point = position.centerPoint(forToast: toast, inSuperview: self)
  135. showToast(toast, duration: duration, point: point, completion: completion)
  136. }
  137. /**
  138. Displays any view as toast at a provided center point and duration. The completion closure
  139. executes when the toast view completes. `didTap` will be `true` if the toast view was
  140. dismissed from a tap.
  141. @param toast The view to be displayed as toast
  142. @param duration The notification duration
  143. @param point The toast's center point
  144. @param completion The completion block, executed after the toast view disappears.
  145. didTap will be `true` if the toast view was dismissed from a tap.
  146. */
  147. func showToast(_ toast: UIView, duration: TimeInterval = ToastManager.shared.duration, point: CGPoint, completion: ((_ didTap: Bool) -> Void)? = nil) {
  148. objc_setAssociatedObject(toast, &ToastKeys.completion, ToastCompletionWrapper(completion), .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  149. if ToastManager.shared.isQueueEnabled, activeToasts.count > 0 {
  150. objc_setAssociatedObject(toast, &ToastKeys.duration, NSNumber(value: duration), .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  151. objc_setAssociatedObject(toast, &ToastKeys.point, NSValue(cgPoint: point), .OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  152. queue.add(toast)
  153. } else {
  154. showToast(toast, duration: duration, point: point)
  155. }
  156. }
  157. // MARK: - Hide Toast Methods
  158. /**
  159. Hides the active toast. If there are multiple toasts active in a view, this method
  160. hides the oldest toast (the first of the toasts to have been presented).
  161. @see `hideAllToasts()` to remove all active toasts from a view.
  162. @warning This method has no effect on activity toasts. Use `hideToastActivity` to
  163. hide activity toasts.
  164. */
  165. func hideToast() {
  166. guard let activeToast = activeToasts.firstObject as? UIView else { return }
  167. hideToast(activeToast)
  168. }
  169. /**
  170. Hides an active toast.
  171. @param toast The active toast view to dismiss. Any toast that is currently being displayed
  172. on the screen is considered active.
  173. @warning this does not clear a toast view that is currently waiting in the queue.
  174. */
  175. func hideToast(_ toast: UIView) {
  176. guard activeToasts.contains(toast) else { return }
  177. hideToast(toast, fromTap: false)
  178. }
  179. /**
  180. Hides all toast views.
  181. @param includeActivity If `true`, toast activity will also be hidden. Default is `false`.
  182. @param clearQueue If `true`, removes all toast views from the queue. Default is `true`.
  183. */
  184. func hideAllToasts(includeActivity: Bool = false, clearQueue: Bool = true) {
  185. if clearQueue {
  186. clearToastQueue()
  187. }
  188. activeToasts.compactMap { $0 as? UIView }
  189. .forEach { hideToast($0) }
  190. if includeActivity {
  191. hideToastActivity()
  192. }
  193. }
  194. /**
  195. Removes all toast views from the queue. This has no effect on toast views that are
  196. active. Use `hideAllToasts(clearQueue:)` to hide the active toasts views and clear
  197. the queue.
  198. */
  199. func clearToastQueue() {
  200. queue.removeAllObjects()
  201. }
  202. // MARK: - Activity Methods
  203. /**
  204. Creates and displays a new toast activity indicator view at a specified position.
  205. @warning Only one toast activity indicator view can be presented per superview. Subsequent
  206. calls to `makeToastActivity(position:)` will be ignored until `hideToastActivity()` is called.
  207. @warning `makeToastActivity(position:)` works independently of the `showToast` methods. Toast
  208. activity views can be presented and dismissed while toast views are being displayed.
  209. `makeToastActivity(position:)` has no effect on the queueing behavior of the `showToast` methods.
  210. @param position The toast's position
  211. */
  212. func makeToastActivity(_ position: ToastPosition) {
  213. // sanity
  214. guard objc_getAssociatedObject(self, &ToastKeys.activityView) as? UIView == nil else { return }
  215. let toast = createToastActivityView()
  216. let point = position.centerPoint(forToast: toast, inSuperview: self)
  217. makeToastActivity(toast, point: point)
  218. }
  219. /**
  220. Creates and displays a new toast activity indicator view at a specified position.
  221. @warning Only one toast activity indicator view can be presented per superview. Subsequent
  222. calls to `makeToastActivity(position:)` will be ignored until `hideToastActivity()` is called.
  223. @warning `makeToastActivity(position:)` works independently of the `showToast` methods. Toast
  224. activity views can be presented and dismissed while toast views are being displayed.
  225. `makeToastActivity(position:)` has no effect on the queueing behavior of the `showToast` methods.
  226. @param point The toast's center point
  227. */
  228. func makeToastActivity(_ point: CGPoint) {
  229. // sanity
  230. guard objc_getAssociatedObject(self, &ToastKeys.activityView) as? UIView == nil else { return }
  231. let toast = createToastActivityView()
  232. makeToastActivity(toast, point: point)
  233. }
  234. /**
  235. Dismisses the active toast activity indicator view.
  236. */
  237. func hideToastActivity() {
  238. if let toast = objc_getAssociatedObject(self, &ToastKeys.activityView) as? UIView {
  239. UIView.animate(withDuration: ToastManager.shared.style.fadeDuration, delay: 0.0, options: [.curveEaseIn, .beginFromCurrentState], animations: {
  240. toast.alpha = 0.0
  241. }) { _ in
  242. toast.removeFromSuperview()
  243. objc_setAssociatedObject(self, &ToastKeys.activityView, nil, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  244. }
  245. }
  246. }
  247. // MARK: - Helper Methods
  248. /**
  249. Returns `true` if a toast view or toast activity view is actively being displayed.
  250. */
  251. func isShowingToast() -> Bool {
  252. return activeToasts.count > 0 || objc_getAssociatedObject(self, &ToastKeys.activityView) != nil
  253. }
  254. // MARK: - Private Activity Methods
  255. private func makeToastActivity(_ toast: UIView, point: CGPoint) {
  256. toast.alpha = 0.0
  257. toast.center = point
  258. objc_setAssociatedObject(self, &ToastKeys.activityView, toast, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  259. self.addSubview(toast)
  260. UIView.animate(withDuration: ToastManager.shared.style.fadeDuration, delay: 0.0, options: .curveEaseOut, animations: {
  261. toast.alpha = 1.0
  262. })
  263. }
  264. private func createToastActivityView() -> UIView {
  265. let style = ToastManager.shared.style
  266. let activityView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: style.activitySize.width, height: style.activitySize.height))
  267. activityView.backgroundColor = style.activityBackgroundColor
  268. activityView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
  269. activityView.layer.cornerRadius = style.cornerRadius
  270. if style.displayShadow {
  271. activityView.layer.shadowColor = style.shadowColor.cgColor
  272. activityView.layer.shadowOpacity = style.shadowOpacity
  273. activityView.layer.shadowRadius = style.shadowRadius
  274. activityView.layer.shadowOffset = style.shadowOffset
  275. }
  276. let activityIndicatorView = UIActivityIndicatorView(style: .whiteLarge)
  277. activityIndicatorView.center = CGPoint(x: activityView.bounds.size.width / 2.0, y: activityView.bounds.size.height / 2.0)
  278. activityView.addSubview(activityIndicatorView)
  279. activityIndicatorView.color = style.activityIndicatorColor
  280. activityIndicatorView.startAnimating()
  281. return activityView
  282. }
  283. // MARK: - Private Show/Hide Methods
  284. private func showToast(_ toast: UIView, duration: TimeInterval, point: CGPoint) {
  285. toast.center = point
  286. toast.alpha = 0.0
  287. if ToastManager.shared.isTapToDismissEnabled {
  288. let recognizer = UITapGestureRecognizer(target: self, action: #selector(UIView.handleToastTapped(_:)))
  289. toast.addGestureRecognizer(recognizer)
  290. toast.isUserInteractionEnabled = true
  291. toast.isExclusiveTouch = true
  292. }
  293. activeToasts.add(toast)
  294. self.addSubview(toast)
  295. let timer = Timer(timeInterval: duration, target: self, selector: #selector(UIView.toastTimerDidFinish(_:)), userInfo: toast, repeats: false)
  296. objc_setAssociatedObject(toast, &ToastKeys.timer, timer, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  297. UIView.animate(withDuration: ToastManager.shared.style.fadeDuration, delay: 0.0, options: [.curveEaseOut, .allowUserInteraction], animations: {
  298. toast.alpha = 1.0
  299. }) { _ in
  300. guard let timer = objc_getAssociatedObject(toast, &ToastKeys.timer) as? Timer else { return }
  301. RunLoop.main.add(timer, forMode: .common)
  302. }
  303. UIAccessibility.post(notification: .screenChanged, argument: toast)
  304. }
  305. private func hideToast(_ toast: UIView, fromTap: Bool) {
  306. if let timer = objc_getAssociatedObject(toast, &ToastKeys.timer) as? Timer {
  307. timer.invalidate()
  308. }
  309. UIView.animate(withDuration: ToastManager.shared.style.fadeDuration, delay: 0.0, options: [.curveEaseIn, .beginFromCurrentState], animations: {
  310. toast.alpha = 0.0
  311. }) { _ in
  312. toast.removeFromSuperview()
  313. self.activeToasts.remove(toast)
  314. if let wrapper = objc_getAssociatedObject(toast, &ToastKeys.completion) as? ToastCompletionWrapper, let completion = wrapper.completion {
  315. completion(fromTap)
  316. }
  317. if let nextToast = self.queue.firstObject as? UIView, let duration = objc_getAssociatedObject(nextToast, &ToastKeys.duration) as? NSNumber, let point = objc_getAssociatedObject(nextToast, &ToastKeys.point) as? NSValue {
  318. self.queue.removeObject(at: 0)
  319. self.showToast(nextToast, duration: duration.doubleValue, point: point.cgPointValue)
  320. }
  321. }
  322. }
  323. // MARK: - Events
  324. @objc
  325. private func handleToastTapped(_ recognizer: UITapGestureRecognizer) {
  326. guard let toast = recognizer.view else { return }
  327. hideToast(toast, fromTap: true)
  328. }
  329. @objc
  330. private func toastTimerDidFinish(_ timer: Timer) {
  331. guard let toast = timer.userInfo as? UIView else { return }
  332. hideToast(toast)
  333. }
  334. // MARK: - Toast Construction
  335. /**
  336. Creates a new toast view with any combination of message, title, and image.
  337. The look and feel is configured via the style. Unlike the `makeToast` methods,
  338. this method does not present the toast view automatically. One of the `showToast`
  339. methods must be used to present the resulting view.
  340. @warning if message, title, and image are all nil, this method will throw
  341. `ToastError.missingParameters`
  342. @param message The message to be displayed
  343. @param title The title
  344. @param image The image
  345. @param style The style. The shared style will be used when nil
  346. @throws `ToastError.missingParameters` when message, title, and image are all nil
  347. @return The newly created toast view
  348. */
  349. func toastViewForMessage(_ message: String?, title: String?, image: UIImage?, style: ToastStyle) throws -> UIView {
  350. // sanity
  351. guard message != nil || title != nil || image != nil else {
  352. throw ToastError.missingParameters
  353. }
  354. var messageLabel: UILabel?
  355. var titleLabel: UILabel?
  356. var imageView: UIImageView?
  357. let wrapperView = UIView()
  358. wrapperView.backgroundColor = style.backgroundColor
  359. wrapperView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin]
  360. wrapperView.layer.cornerRadius = style.cornerRadius
  361. if style.displayShadow {
  362. wrapperView.layer.shadowColor = style.shadowColor.cgColor
  363. wrapperView.layer.shadowOpacity = style.shadowOpacity
  364. wrapperView.layer.shadowRadius = style.shadowRadius
  365. wrapperView.layer.shadowOffset = style.shadowOffset
  366. }
  367. if let image = image {
  368. imageView = UIImageView(image: image)
  369. imageView?.contentMode = .scaleAspectFit
  370. imageView?.frame = CGRect(x: style.horizontalPadding, y: style.verticalPadding, width: style.imageSize.width, height: style.imageSize.height)
  371. }
  372. var imageRect = CGRect.zero
  373. if let imageView = imageView {
  374. imageRect.origin.x = style.horizontalPadding
  375. imageRect.origin.y = style.verticalPadding
  376. imageRect.size.width = imageView.bounds.size.width
  377. imageRect.size.height = imageView.bounds.size.height
  378. }
  379. if let title = title {
  380. titleLabel = UILabel()
  381. titleLabel?.numberOfLines = style.titleNumberOfLines
  382. titleLabel?.font = style.titleFont
  383. titleLabel?.textAlignment = style.titleAlignment
  384. titleLabel?.lineBreakMode = .byTruncatingTail
  385. titleLabel?.textColor = style.titleColor
  386. titleLabel?.backgroundColor = UIColor.clear
  387. titleLabel?.text = title;
  388. let maxTitleSize = CGSize(width: (self.bounds.size.width * style.maxWidthPercentage) - imageRect.size.width, height: self.bounds.size.height * style.maxHeightPercentage)
  389. let titleSize = titleLabel?.sizeThatFits(maxTitleSize)
  390. if let titleSize = titleSize {
  391. titleLabel?.frame = CGRect(x: 0.0, y: 0.0, width: titleSize.width, height: titleSize.height)
  392. }
  393. }
  394. if let message = message {
  395. messageLabel = UILabel()
  396. messageLabel?.text = message
  397. messageLabel?.numberOfLines = style.messageNumberOfLines
  398. messageLabel?.font = style.messageFont
  399. messageLabel?.textAlignment = style.messageAlignment
  400. messageLabel?.lineBreakMode = .byTruncatingTail;
  401. messageLabel?.textColor = style.messageColor
  402. messageLabel?.backgroundColor = UIColor.clear
  403. let maxMessageSize = CGSize(width: (self.bounds.size.width * style.maxWidthPercentage) - imageRect.size.width, height: self.bounds.size.height * style.maxHeightPercentage)
  404. let messageSize = messageLabel?.sizeThatFits(maxMessageSize)
  405. if let messageSize = messageSize {
  406. let actualWidth = min(messageSize.width, maxMessageSize.width)
  407. let actualHeight = min(messageSize.height, maxMessageSize.height)
  408. messageLabel?.frame = CGRect(x: 0.0, y: 0.0, width: actualWidth, height: actualHeight)
  409. }
  410. }
  411. var titleRect = CGRect.zero
  412. if let titleLabel = titleLabel {
  413. titleRect.origin.x = imageRect.origin.x + imageRect.size.width + style.horizontalPadding
  414. titleRect.origin.y = style.verticalPadding
  415. titleRect.size.width = titleLabel.bounds.size.width
  416. titleRect.size.height = titleLabel.bounds.size.height
  417. }
  418. var messageRect = CGRect.zero
  419. if let messageLabel = messageLabel {
  420. messageRect.origin.x = imageRect.origin.x + imageRect.size.width + style.horizontalPadding
  421. messageRect.origin.y = titleRect.origin.y + titleRect.size.height + style.verticalPadding
  422. messageRect.size.width = messageLabel.bounds.size.width
  423. messageRect.size.height = messageLabel.bounds.size.height
  424. }
  425. let longerWidth = max(titleRect.size.width, messageRect.size.width)
  426. let longerX = max(titleRect.origin.x, messageRect.origin.x)
  427. let wrapperWidth = max((imageRect.size.width + (style.horizontalPadding * 2.0)), (longerX + longerWidth + style.horizontalPadding))
  428. let textMaxY = messageRect.size.height <= 0.0 && titleRect.size.height > 0.0 ? titleRect.maxY : messageRect.maxY
  429. let wrapperHeight = max((textMaxY + style.verticalPadding), (imageRect.size.height + (style.verticalPadding * 2.0)))
  430. wrapperView.frame = CGRect(x: 0.0, y: 0.0, width: wrapperWidth, height: wrapperHeight)
  431. if let titleLabel = titleLabel {
  432. titleRect.size.width = longerWidth
  433. titleLabel.frame = titleRect
  434. wrapperView.addSubview(titleLabel)
  435. }
  436. if let messageLabel = messageLabel {
  437. messageRect.size.width = longerWidth
  438. messageLabel.frame = messageRect
  439. wrapperView.addSubview(messageLabel)
  440. }
  441. if let imageView = imageView {
  442. wrapperView.addSubview(imageView)
  443. }
  444. return wrapperView
  445. }
  446. }
  447. // MARK: - Toast Style
  448. /**
  449. `ToastStyle` instances define the look and feel for toast views created via the
  450. `makeToast` methods as well for toast views created directly with
  451. `toastViewForMessage(message:title:image:style:)`.
  452. @warning `ToastStyle` offers relatively simple styling options for the default
  453. toast view. If you require a toast view with more complex UI, it probably makes more
  454. sense to create your own custom UIView subclass and present it with the `showToast`
  455. methods.
  456. */
  457. public struct ToastStyle {
  458. public init() {}
  459. /**
  460. The background color. Default is `.black` at 80% opacity.
  461. */
  462. public var backgroundColor: UIColor = UIColor.black.withAlphaComponent(0.8)
  463. /**
  464. The title color. Default is `UIColor.whiteColor()`.
  465. */
  466. public var titleColor: UIColor = .white
  467. /**
  468. The message color. Default is `.white`.
  469. */
  470. public var messageColor: UIColor = .white
  471. /**
  472. A percentage value from 0.0 to 1.0, representing the maximum width of the toast
  473. view relative to it's superview. Default is 0.8 (80% of the superview's width).
  474. */
  475. public var maxWidthPercentage: CGFloat = 0.8 {
  476. didSet {
  477. maxWidthPercentage = max(min(maxWidthPercentage, 1.0), 0.0)
  478. }
  479. }
  480. /**
  481. A percentage value from 0.0 to 1.0, representing the maximum height of the toast
  482. view relative to it's superview. Default is 0.8 (80% of the superview's height).
  483. */
  484. public var maxHeightPercentage: CGFloat = 0.8 {
  485. didSet {
  486. maxHeightPercentage = max(min(maxHeightPercentage, 1.0), 0.0)
  487. }
  488. }
  489. /**
  490. The spacing from the horizontal edge of the toast view to the content. When an image
  491. is present, this is also used as the padding between the image and the text.
  492. Default is 10.0.
  493. */
  494. public var horizontalPadding: CGFloat = 10.0
  495. /**
  496. The spacing from the vertical edge of the toast view to the content. When a title
  497. is present, this is also used as the padding between the title and the message.
  498. Default is 10.0. On iOS11+, this value is added added to the `safeAreaInset.top`
  499. and `safeAreaInsets.bottom`.
  500. */
  501. public var verticalPadding: CGFloat = 10.0
  502. /**
  503. The corner radius. Default is 10.0.
  504. */
  505. public var cornerRadius: CGFloat = 10.0;
  506. /**
  507. The title font. Default is `.boldSystemFont(16.0)`.
  508. */
  509. public var titleFont: UIFont = .boldSystemFont(ofSize: 16.0)
  510. /**
  511. The message font. Default is `.systemFont(ofSize: 16.0)`.
  512. */
  513. public var messageFont: UIFont = .systemFont(ofSize: 16.0)
  514. /**
  515. The title text alignment. Default is `NSTextAlignment.Left`.
  516. */
  517. public var titleAlignment: NSTextAlignment = .left
  518. /**
  519. The message text alignment. Default is `NSTextAlignment.Left`.
  520. */
  521. public var messageAlignment: NSTextAlignment = .left
  522. /**
  523. The maximum number of lines for the title. The default is 0 (no limit).
  524. */
  525. public var titleNumberOfLines = 0
  526. /**
  527. The maximum number of lines for the message. The default is 0 (no limit).
  528. */
  529. public var messageNumberOfLines = 0
  530. /**
  531. Enable or disable a shadow on the toast view. Default is `false`.
  532. */
  533. public var displayShadow = false
  534. /**
  535. The shadow color. Default is `.black`.
  536. */
  537. public var shadowColor: UIColor = .black
  538. /**
  539. A value from 0.0 to 1.0, representing the opacity of the shadow.
  540. Default is 0.8 (80% opacity).
  541. */
  542. public var shadowOpacity: Float = 0.8 {
  543. didSet {
  544. shadowOpacity = max(min(shadowOpacity, 1.0), 0.0)
  545. }
  546. }
  547. /**
  548. The shadow radius. Default is 6.0.
  549. */
  550. public var shadowRadius: CGFloat = 6.0
  551. /**
  552. The shadow offset. The default is 4 x 4.
  553. */
  554. public var shadowOffset = CGSize(width: 4.0, height: 4.0)
  555. /**
  556. The image size. The default is 80 x 80.
  557. */
  558. public var imageSize = CGSize(width: 80.0, height: 80.0)
  559. /**
  560. The size of the toast activity view when `makeToastActivity(position:)` is called.
  561. Default is 100 x 100.
  562. */
  563. public var activitySize = CGSize(width: 100.0, height: 100.0)
  564. /**
  565. The fade in/out animation duration. Default is 0.2.
  566. */
  567. public var fadeDuration: TimeInterval = 0.2
  568. /**
  569. Activity indicator color. Default is `.white`.
  570. */
  571. public var activityIndicatorColor: UIColor = .white
  572. /**
  573. Activity background color. Default is `.black` at 80% opacity.
  574. */
  575. public var activityBackgroundColor: UIColor = UIColor.black.withAlphaComponent(0.8)
  576. }
  577. // MARK: - Toast Manager
  578. /**
  579. `ToastManager` provides general configuration options for all toast
  580. notifications. Backed by a singleton instance.
  581. */
  582. public class ToastManager {
  583. /**
  584. The `ToastManager` singleton instance.
  585. */
  586. public static let shared = ToastManager()
  587. /**
  588. The shared style. Used whenever toastViewForMessage(message:title:image:style:) is called
  589. with with a nil style.
  590. */
  591. public var style = ToastStyle()
  592. /**
  593. Enables or disables tap to dismiss on toast views. Default is `true`.
  594. */
  595. public var isTapToDismissEnabled = true
  596. /**
  597. Enables or disables queueing behavior for toast views. When `true`,
  598. toast views will appear one after the other. When `false`, multiple toast
  599. views will appear at the same time (potentially overlapping depending
  600. on their positions). This has no effect on the toast activity view,
  601. which operates independently of normal toast views. Default is `false`.
  602. */
  603. public var isQueueEnabled = false
  604. /**
  605. The default duration. Used for the `makeToast` and
  606. `showToast` methods that don't require an explicit duration.
  607. Default is 3.0.
  608. */
  609. public var duration: TimeInterval = 3.0
  610. /**
  611. Sets the default position. Used for the `makeToast` and
  612. `showToast` methods that don't require an explicit position.
  613. Default is `ToastPosition.Bottom`.
  614. */
  615. public var position: ToastPosition = .bottom
  616. }
  617. // MARK: - ToastPosition
  618. public enum ToastPosition {
  619. case top
  620. case center
  621. case bottom
  622. fileprivate func centerPoint(forToast toast: UIView, inSuperview superview: UIView) -> CGPoint {
  623. let topPadding: CGFloat = ToastManager.shared.style.verticalPadding + superview.csSafeAreaInsets.top
  624. let bottomPadding: CGFloat = ToastManager.shared.style.verticalPadding + superview.csSafeAreaInsets.bottom
  625. switch self {
  626. case .top:
  627. return CGPoint(x: superview.bounds.size.width / 2.0, y: (toast.frame.size.height / 2.0) + topPadding)
  628. case .center:
  629. return CGPoint(x: superview.bounds.size.width / 2.0, y: superview.bounds.size.height / 2.0)
  630. case .bottom:
  631. return CGPoint(x: superview.bounds.size.width / 2.0, y: (superview.bounds.size.height - (toast.frame.size.height / 2.0)) - bottomPadding)
  632. }
  633. }
  634. }
  635. // MARK: - Private UIView Extensions
  636. private extension UIView {
  637. var csSafeAreaInsets: UIEdgeInsets {
  638. if #available(iOS 11.0, *) {
  639. return self.safeAreaInsets
  640. } else {
  641. return .zero
  642. }
  643. }
  644. }