From 15752b9044d6347d157da1a0922711cfd74a15eb Mon Sep 17 00:00:00 2001 From: Fabian Schuetz Date: Wed, 7 Oct 2015 11:59:01 +0200 Subject: [PATCH 1/6] update to swift2 --- Toast/Toast/Toast.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Toast/Toast/Toast.swift b/Toast/Toast/Toast.swift index 56ecda3..da01f6d 100644 --- a/Toast/Toast/Toast.swift +++ b/Toast/Toast/Toast.swift @@ -57,7 +57,7 @@ public class Toast { public func show() -> Void { let keyWindow = UIApplication.sharedApplication().keyWindow - if let windowView = keyWindow?.subviews.first as? UIView { + if let windowView = keyWindow?.subviews.first as UIView? { toast = ToastView() toast.textLabel?.text = self.text @@ -73,8 +73,8 @@ public class Toast { windowView.addSubview(toast) - let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[toast]-\(yMargin)-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) - let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=\(margin))-[toast]-(>=\(margin))-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) + let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[toast]-\(yMargin)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) + let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=\(margin))-[toast]-(>=\(margin))-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) let centerContraint = NSLayoutConstraint(item: toast, attribute: .CenterX, relatedBy: .Equal, toItem: windowView, attribute: .CenterX, multiplier: 1, constant: 0) windowView.addConstraints(verticalConstraints) @@ -126,4 +126,4 @@ public class Toast { public func remove() -> Void { self.toast.removeFromSuperview() } -} \ No newline at end of file +} From b8209471e17a02405517e63df1d117dd748bcba3 Mon Sep 17 00:00:00 2001 From: Fabian Schuetz Date: Wed, 7 Oct 2015 11:59:30 +0200 Subject: [PATCH 2/6] udpate to swift2 --- Toast/Toast/ToastView.swift | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Toast/Toast/ToastView.swift b/Toast/Toast/ToastView.swift index 23d355c..32876a4 100644 --- a/Toast/Toast/ToastView.swift +++ b/Toast/Toast/ToastView.swift @@ -25,18 +25,18 @@ class ToastView: UIView { } required init(coder aDecoder: NSCoder) { - super.init(coder: aDecoder) + super.init(coder: aDecoder)! setup() } private func setup() { - self.setTranslatesAutoresizingMaskIntoConstraints(false) + //self.setTranslatesAutoresizingMaskIntoConstraints(false) self.alpha = 0 if Toast.appearance.blur { let blurEffect = UIBlurEffect(style: Toast.appearance.blurStyle) blurEffectView = UIVisualEffectView(effect: blurEffect) - blurEffectView?.setTranslatesAutoresizingMaskIntoConstraints(false) + //blurEffectView?.setTranslatesAutoresizingMaskIntoConstraints(false) blurEffectView?.layer.cornerRadius = 5 blurEffectView?.clipsToBounds = true @@ -46,7 +46,7 @@ class ToastView: UIView { textLabel = UILabel() textLabel?.textColor = Toast.appearance.textColor textLabel?.numberOfLines = 0 - textLabel?.setTranslatesAutoresizingMaskIntoConstraints(false) + //textLabel?.setTranslatesAutoresizingMaskIntoConstraints(false) self.addSubview(textLabel!) } @@ -57,8 +57,8 @@ class ToastView: UIView { var views: [String: AnyObject] = ["label": textLabel!] - let horizontalMargin = NSLayoutConstraint.constraintsWithVisualFormat("H:|-\(padding)-[label]-\(padding)-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) - let verticalMargin = NSLayoutConstraint.constraintsWithVisualFormat("V:|-\(padding)-[label]-\(padding)-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) + let horizontalMargin = NSLayoutConstraint.constraintsWithVisualFormat("H:|-\(padding)-[label]-\(padding)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) + let verticalMargin = NSLayoutConstraint.constraintsWithVisualFormat("V:|-\(padding)-[label]-\(padding)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) self.addConstraints(horizontalMargin) self.addConstraints(verticalMargin) @@ -66,8 +66,8 @@ class ToastView: UIView { if blurEffectView != nil { views["blur"] = blurEffectView! - let blurWidthConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|[blur]|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) - let blurHeightContraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|[blur]|", options: NSLayoutFormatOptions(0), metrics: nil, views: views) + let blurWidthConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|[blur]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) + let blurHeightContraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|[blur]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) self.addConstraints(blurWidthConstraint) self.addConstraints(blurHeightContraint) @@ -78,4 +78,4 @@ class ToastView: UIView { super.updateConstraints() } -} \ No newline at end of file +} From 58826e9fe01f1b7702d1ef77ca5e3aa94a2c89f1 Mon Sep 17 00:00:00 2001 From: fscz Date: Sun, 10 Apr 2016 11:24:37 +0200 Subject: [PATCH 3/6] switched layouting from autolayout to manual computation --- Toast/Toast/Toast.swift | 29 ++------- Toast/Toast/ToastAppearance.swift | 13 ++-- Toast/Toast/ToastView.swift | 100 ++++++++++++++++++------------ 3 files changed, 70 insertions(+), 72 deletions(-) diff --git a/Toast/Toast/Toast.swift b/Toast/Toast/Toast.swift index da01f6d..c1a16da 100644 --- a/Toast/Toast/Toast.swift +++ b/Toast/Toast/Toast.swift @@ -16,7 +16,7 @@ public class Toast { public static let appearance = ToastAppearance() /// Shared keyboard observer used to determine appropriate toast position - private static var keyboardObserver: KeyboardObserver? + static var keyboardObserver: KeyboardObserver? var text: String! var duration: Double! @@ -55,32 +55,15 @@ public class Toast { :returns: Void */ public func show() -> Void { - let keyWindow = UIApplication.sharedApplication().keyWindow - - if let windowView = keyWindow?.subviews.first as UIView? { - toast = ToastView() - toast.textLabel?.text = self.text - - let margin = Toast.appearance.margin - let views = ["toast": toast] - let yMargin: CGFloat + let nc = UIApplication.sharedApplication().keyWindow!.rootViewController as! UINavigationController + let vc = nc.viewControllers.last + + if let windowView = vc?.view { + toast = ToastView(text: self.text, parent: windowView) - if let kO = Toast.keyboardObserver { - yMargin = margin + kO.offset - } else { - yMargin = margin - } windowView.addSubview(toast) - let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[toast]-\(yMargin)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) - let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=\(margin))-[toast]-(>=\(margin))-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) - let centerContraint = NSLayoutConstraint(item: toast, attribute: .CenterX, relatedBy: .Equal, toItem: windowView, attribute: .CenterX, multiplier: 1, constant: 0) - - windowView.addConstraints(verticalConstraints) - windowView.addConstraints(horizontalConstraints) - windowView.addConstraint(centerContraint) - UIView.animateWithDuration(Toast.appearance.animationDuration, animations: { () -> Void in self.toast.alpha = 1 }) diff --git a/Toast/Toast/ToastAppearance.swift b/Toast/Toast/ToastAppearance.swift index a03f5da..cdc6947 100644 --- a/Toast/Toast/ToastAppearance.swift +++ b/Toast/Toast/ToastAppearance.swift @@ -9,24 +9,21 @@ import Foundation public class ToastAppearance { - /// Whether or not the toast background should have a blurred background - public var blur: Bool = true - - /// Blur style if blur is set to true - public var blurStyle: UIBlurEffectStyle = .Dark /// Toast background corner radius public var cornerRadius: CGFloat = 4 - /// Margin between the toast and the surrounding view - public var margin: CGFloat = 16 - /// Padding between the text label and the toast background public var padding: CGFloat = 10 + /// The label text color + public var color: UIColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7) + /// The label text color public var textColor: UIColor = UIColor.whiteColor() /// The duration of the fade in animation public var animationDuration: NSTimeInterval = 0.5 + + public var font: UIFont = UIFont(name: "Roboto-Regular", size: 20)! } diff --git a/Toast/Toast/ToastView.swift b/Toast/Toast/ToastView.swift index 32876a4..2c96b6d 100644 --- a/Toast/Toast/ToastView.swift +++ b/Toast/Toast/ToastView.swift @@ -8,12 +8,29 @@ import Foundation +extension UILabel{ + + func requiredHeight() -> CGFloat{ + + let label:UILabel = UILabel(frame: CGRectMake(0, 0, self.frame.width, CGFloat.max)) + label.numberOfLines = 0 + label.lineBreakMode = NSLineBreakMode.ByWordWrapping + label.font = self.font + label.text = self.text + + label.sizeToFit() + + return label.frame.height + } +} + class ToastView: UIView { - var blurEffectView: UIVisualEffectView? var textLabel: UILabel? private var constraintsSet: Bool = false + private var ko = KeyboardObserver() + required init() { super.init(frame: CGRectZero) setup() @@ -29,53 +46,54 @@ class ToastView: UIView { setup() } - private func setup() { - //self.setTranslatesAutoresizingMaskIntoConstraints(false) - self.alpha = 0 + init(text: String, parent: UIView) { + super.init(frame: CGRectZero) + setupWithText(text, parent: parent) + } + + private func setupWithText(text: String, parent: UIView) { + setup() + + frame = CGRectMake(10, parent.frame.size.height, parent.frame.size.width - 20, 100) + + textLabel?.frame = CGRectMake(0 + Toast.appearance.padding, + 0 + Toast.appearance.padding, + frame.size.width - Toast.appearance.padding * 2, + 1000) + textLabel?.text = text + let size = textLabel?.sizeThatFits(CGSizeMake(frame.size.width - Toast.appearance.padding * 2, 1000)) + textLabel!.frame = CGRectMake( textLabel!.frame.origin.x, + textLabel!.frame.origin.y, + textLabel!.frame.size.width, + size!.height + ) + + let yMargin: CGFloat - if Toast.appearance.blur { - let blurEffect = UIBlurEffect(style: Toast.appearance.blurStyle) - blurEffectView = UIVisualEffectView(effect: blurEffect) - //blurEffectView?.setTranslatesAutoresizingMaskIntoConstraints(false) - blurEffectView?.layer.cornerRadius = 5 - blurEffectView?.clipsToBounds = true - - self.addSubview(blurEffectView!) + if let kO = Toast.keyboardObserver { + yMargin = kO.offset + } else { + yMargin = 0 } + frame = CGRectMake(frame.origin.x, + frame.origin.y - textLabel!.frame.size.height - Toast.appearance.padding * 2 - 10 - yMargin, + frame.size.width, + textLabel!.frame.size.height + Toast.appearance.padding * 2) + } + + private func setup() { + self.alpha = 0 + self.backgroundColor = Toast.appearance.color + self.layer.cornerRadius = Toast.appearance.cornerRadius + textLabel = UILabel() textLabel?.textColor = Toast.appearance.textColor textLabel?.numberOfLines = 0 - //textLabel?.setTranslatesAutoresizingMaskIntoConstraints(false) + textLabel?.textAlignment = NSTextAlignment.Center + textLabel?.font = Toast.appearance.font self.addSubview(textLabel!) } - - override func updateConstraints() { - if !constraintsSet { - let padding = Toast.appearance.padding - - var views: [String: AnyObject] = ["label": textLabel!] - - let horizontalMargin = NSLayoutConstraint.constraintsWithVisualFormat("H:|-\(padding)-[label]-\(padding)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) - let verticalMargin = NSLayoutConstraint.constraintsWithVisualFormat("V:|-\(padding)-[label]-\(padding)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) - - self.addConstraints(horizontalMargin) - self.addConstraints(verticalMargin) - - if blurEffectView != nil { - views["blur"] = blurEffectView! - - let blurWidthConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|[blur]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) - let blurHeightContraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|[blur]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) - - self.addConstraints(blurWidthConstraint) - self.addConstraints(blurHeightContraint) - } - - constraintsSet = true - } - - super.updateConstraints() - } + } From f12f56ceb5035c292f48b338cbde91ec1673c5ac Mon Sep 17 00:00:00 2001 From: fscz Date: Sun, 10 Apr 2016 11:48:07 +0200 Subject: [PATCH 4/6] made appearance configurable on per-toast basis --- Toast/Toast/Toast.swift | 17 ++++++----- Toast/Toast/ToastAppearance.swift | 9 ++++-- Toast/Toast/ToastView.swift | 48 ++++++++++++++----------------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/Toast/Toast/Toast.swift b/Toast/Toast/Toast.swift index c1a16da..68cd1a3 100644 --- a/Toast/Toast/Toast.swift +++ b/Toast/Toast/Toast.swift @@ -13,7 +13,7 @@ public class Toast { public static let LENGTH_LONG: Double = 5 /// Shared toast appearance settings - public static let appearance = ToastAppearance() + let appearance: ToastAppearance! /// Shared keyboard observer used to determine appropriate toast position static var keyboardObserver: KeyboardObserver? @@ -22,6 +22,10 @@ public class Toast { var duration: Double! var toast: ToastView! + public init(appearance: ToastAppearance) { + self.appearance = appearance + } + /** Initializes keyboard observer used to figure out the appropriate toast position @@ -40,8 +44,8 @@ public class Toast { :returns: Toast */ - public class func makeText(text: String, duration: Double = Toast.LENGTH_LONG) -> Toast { - let toast = Toast() + public class func makeText(text: String, duration: Double = Toast.LENGTH_LONG, appearance: ToastAppearance = ToastAppearance()) -> Toast { + let toast = Toast(appearance: appearance) toast.text = text toast.duration = duration @@ -59,12 +63,11 @@ public class Toast { let vc = nc.viewControllers.last if let windowView = vc?.view { - toast = ToastView(text: self.text, parent: windowView) - + toast = ToastView(text: self.text, parent: windowView, appearance: appearance) windowView.addSubview(toast) - UIView.animateWithDuration(Toast.appearance.animationDuration, animations: { () -> Void in + UIView.animateWithDuration(appearance.animationDuration, animations: { () -> Void in self.toast.alpha = 1 }) @@ -78,7 +81,7 @@ public class Toast { :returns: Void */ public func hide() -> Void { - UIView.animateWithDuration(Toast.appearance.animationDuration, animations: { () -> Void in + UIView.animateWithDuration(appearance.animationDuration, animations: { () -> Void in self.toast.alpha = 0 }) { (_) -> Void in self.remove() diff --git a/Toast/Toast/ToastAppearance.swift b/Toast/Toast/ToastAppearance.swift index cdc6947..1e1265c 100644 --- a/Toast/Toast/ToastAppearance.swift +++ b/Toast/Toast/ToastAppearance.swift @@ -10,6 +10,10 @@ import Foundation public class ToastAppearance { + public init() { + + } + /// Toast background corner radius public var cornerRadius: CGFloat = 4 @@ -17,7 +21,7 @@ public class ToastAppearance { public var padding: CGFloat = 10 /// The label text color - public var color: UIColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7) + public var backgroundColor: UIColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.7) /// The label text color public var textColor: UIColor = UIColor.whiteColor() @@ -25,5 +29,6 @@ public class ToastAppearance { /// The duration of the fade in animation public var animationDuration: NSTimeInterval = 0.5 - public var font: UIFont = UIFont(name: "Roboto-Regular", size: 20)! + /// A font to use + public var font: UIFont? = nil } diff --git a/Toast/Toast/ToastView.swift b/Toast/Toast/ToastView.swift index 2c96b6d..2982334 100644 --- a/Toast/Toast/ToastView.swift +++ b/Toast/Toast/ToastView.swift @@ -33,35 +33,43 @@ class ToastView: UIView { required init() { super.init(frame: CGRectZero) - setup() } required override init(frame: CGRect) { super.init(frame: frame) - setup() } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! - setup() } - init(text: String, parent: UIView) { + init(text: String, parent: UIView, appearance: ToastAppearance) { super.init(frame: CGRectZero) - setupWithText(text, parent: parent) + setup(text, parent: parent, appearance: appearance) } - private func setupWithText(text: String, parent: UIView) { - setup() + private func setup(text: String, parent: UIView, appearance: ToastAppearance) { + + self.alpha = 0 + self.backgroundColor = appearance.backgroundColor + self.layer.cornerRadius = appearance.cornerRadius + + textLabel = UILabel() + textLabel?.textColor = appearance.textColor + textLabel?.numberOfLines = 0 + textLabel?.textAlignment = NSTextAlignment.Center + textLabel?.font = appearance.font + + self.addSubview(textLabel!) frame = CGRectMake(10, parent.frame.size.height, parent.frame.size.width - 20, 100) - textLabel?.frame = CGRectMake(0 + Toast.appearance.padding, - 0 + Toast.appearance.padding, - frame.size.width - Toast.appearance.padding * 2, + textLabel?.frame = CGRectMake(0 + appearance.padding, + 0 + appearance.padding, + frame.size.width - appearance.padding * 2, 1000) textLabel?.text = text - let size = textLabel?.sizeThatFits(CGSizeMake(frame.size.width - Toast.appearance.padding * 2, 1000)) + let size = textLabel?.sizeThatFits(CGSizeMake(frame.size.width - appearance.padding * 2, 1000)) textLabel!.frame = CGRectMake( textLabel!.frame.origin.x, textLabel!.frame.origin.y, textLabel!.frame.size.width, @@ -77,23 +85,9 @@ class ToastView: UIView { } frame = CGRectMake(frame.origin.x, - frame.origin.y - textLabel!.frame.size.height - Toast.appearance.padding * 2 - 10 - yMargin, + frame.origin.y - textLabel!.frame.size.height - appearance.padding * 2 - 10 - yMargin, frame.size.width, - textLabel!.frame.size.height + Toast.appearance.padding * 2) - } - - private func setup() { - self.alpha = 0 - self.backgroundColor = Toast.appearance.color - self.layer.cornerRadius = Toast.appearance.cornerRadius - - textLabel = UILabel() - textLabel?.textColor = Toast.appearance.textColor - textLabel?.numberOfLines = 0 - textLabel?.textAlignment = NSTextAlignment.Center - textLabel?.font = Toast.appearance.font - - self.addSubview(textLabel!) + textLabel!.frame.size.height + appearance.padding * 2) } } From 0cd42a7a611204b067247b116863a806feb67e50 Mon Sep 17 00:00:00 2001 From: fscz Date: Sun, 10 Apr 2016 12:22:43 +0200 Subject: [PATCH 5/6] podspec to new layouting --- SimpleToast.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleToast.podspec b/SimpleToast.podspec index 2a61170..d0b7cd7 100644 --- a/SimpleToast.podspec +++ b/SimpleToast.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SimpleToast' - s.version = '0.1.1' + s.version = '0.2.0' s.license = 'MIT' s.summary = 'Simple toasts with Android-like API and keyboard aware positioning.' s.homepage = 'https://github.com/lohmander/SimpleToast' From dcb5ef067a37acbb9aff380a426fdb7c0a52948b Mon Sep 17 00:00:00 2001 From: fscz Date: Sun, 10 Apr 2016 12:26:22 +0200 Subject: [PATCH 6/6] set podspec to head --- SimpleToast.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimpleToast.podspec b/SimpleToast.podspec index d0b7cd7..ddb212d 100644 --- a/SimpleToast.podspec +++ b/SimpleToast.podspec @@ -5,7 +5,7 @@ Pod::Spec.new do |s| s.summary = 'Simple toasts with Android-like API and keyboard aware positioning.' s.homepage = 'https://github.com/lohmander/SimpleToast' s.authors = { 'Hannes Lohmander' => 'hannes@lohmander.me' } - s.source = { :git => 'https://github.com/lohmander/SimpleToast.git', :tag => s.version } + s.source = { :git => 'https://github.com/lohmander/SimpleToast.git' } s.ios.deployment_target = '8.0'