From d371066f484c51ea1eb8a1b6711156feb8792582 Mon Sep 17 00:00:00 2001 From: Anton Gubarenko Date: Fri, 12 Jan 2024 01:21:30 +0300 Subject: [PATCH] Image tint color added --- Example/ViewController.swift | 28 +++++++++++++++++++--------- Toast/Toast.swift | 10 +++++++++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/Example/ViewController.swift b/Example/ViewController.swift index bbd7cc8..891adc8 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -144,11 +144,12 @@ extension ViewController { case 3: cell.textLabel?.text = "Make toast with an image" case 4: cell.textLabel?.text = "Make toast with a title, image, and completion closure" case 5: cell.textLabel?.text = "Make toast with a custom style" - case 6: cell.textLabel?.text = "Show a custom view as toast" - case 7: cell.textLabel?.text = "Show an image as toast at point\n(110, 110)" - case 8: cell.textLabel?.text = showingActivity ? "Hide toast activity" : "Show toast activity" - case 9: cell.textLabel?.text = "Hide toast" - case 10: cell.textLabel?.text = "Hide all toasts" + case 6: cell.textLabel?.text = "Make toast with a custom style and image" + case 7: cell.textLabel?.text = "Show a custom view as toast" + case 8: cell.textLabel?.text = "Show an image as toast at point\n(110, 110)" + case 9: cell.textLabel?.text = showingActivity ? "Hide toast activity" : "Show toast activity" + case 10: cell.textLabel?.text = "Hide toast" + case 11: cell.textLabel?.text = "Hide all toasts" default: cell.textLabel?.text = nil } @@ -193,16 +194,25 @@ extension ViewController { style.backgroundColor = UIColor.yellow self.navigationController?.view.makeToast("This is a piece of toast with a custom style", duration: 3.0, position: .bottom, style: style) case 6: + // Make toast with a custom style and image + var style = ToastStyle() + style.messageFont = UIFont(name: "Zapfino", size: 14.0)! + style.messageColor = UIColor.red + style.messageAlignment = .center + style.backgroundColor = UIColor.yellow + style.imageTint = .red + self.navigationController?.view.makeToast("This is a piece of toast with a custom style", duration: 3.0, position: .bottom, image: UIImage(named: "toast.png"), style: style) + case 7: // Show a custom view as toast let customView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 80.0, height: 400.0)) customView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin] customView.backgroundColor = .lightBlue self.navigationController?.view.showToast(customView, duration: 2.0, position: .center) - case 7: + case 8: // Show an image view as toast, on center at point (110,110) let toastView = UIImageView(image: UIImage(named: "toast.png")) self.navigationController?.view.showToast(toastView, duration: 2.0, point: CGPoint(x: 110.0, y: 110.0)) - case 8: + case 9: // Make toast activity if !showingActivity { self.navigationController?.view.makeToastActivity(.center) @@ -213,10 +223,10 @@ extension ViewController { showingActivity.toggle() tableView.reloadData() - case 9: + case 10: // Hide toast self.navigationController?.view.hideToast() - case 10: + case 11: // Hide all toasts self.navigationController?.view.hideAllToasts() default: diff --git a/Toast/Toast.swift b/Toast/Toast.swift index 5b4dd05..992d43b 100644 --- a/Toast/Toast.swift +++ b/Toast/Toast.swift @@ -447,9 +447,12 @@ public extension UIView { } if let image = image { - imageView = UIImageView(image: image) + imageView = UIImageView(image: style.imageTint == .clear ? image : image.withRenderingMode(.alwaysTemplate)) imageView?.contentMode = .scaleAspectFit imageView?.frame = CGRect(x: style.horizontalPadding, y: style.verticalPadding, width: style.imageSize.width, height: style.imageSize.height) + if style.imageTint != .clear { + imageView?.tintColor = style.imageTint + } } var imageRect = CGRect.zero @@ -682,6 +685,11 @@ public struct ToastStyle { */ public var imageSize = CGSize(width: 80.0, height: 80.0) + /** + The image tint color. If not .clear then image will be rendered as template and tint will be applied. Default is .clear. + */ + public var imageTint = UIColor.clear + /** The size of the toast activity view when `makeToastActivity(position:)` is called. Default is 100 x 100.