Last active
December 21, 2017 16:24
-
-
Save buzan92/bc2aee1475cf004c392e34fc209721ab to your computer and use it in GitHub Desktop.
Toast notification class for swift 4 like android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
public extension UIView { | |
public func showToast(message:String, duration:Int = 2000) { | |
let toastLabel = UIPaddingLabel(); | |
toastLabel.padding = 10; | |
toastLabel.translatesAutoresizingMaskIntoConstraints = false; | |
toastLabel.backgroundColor = UIColor.darkGray; | |
toastLabel.textColor = UIColor.white; | |
toastLabel.textAlignment = .center; | |
toastLabel.text = message; | |
toastLabel.numberOfLines = 0; | |
toastLabel.alpha = 0.9; | |
toastLabel.layer.cornerRadius = 20; | |
toastLabel.clipsToBounds = true; | |
self.addSubview(toastLabel); | |
self.addConstraint(NSLayoutConstraint(item:toastLabel, attribute:.left, relatedBy:.greaterThanOrEqual, toItem:self, attribute:.left, multiplier:1, constant:20)); | |
self.addConstraint(NSLayoutConstraint(item:toastLabel, attribute:.right, relatedBy:.lessThanOrEqual, toItem:self, attribute:.right, multiplier:1, constant:-20)); | |
self.addConstraint(NSLayoutConstraint(item:toastLabel, attribute:.bottom, relatedBy:.equal, toItem:self, attribute:.bottom, multiplier:1, constant:-20)); | |
self.addConstraint(NSLayoutConstraint(item:toastLabel, attribute:.centerX, relatedBy:.equal, toItem:self, attribute:.centerX, multiplier:1, constant:0)); | |
UIView.animate(withDuration:0.5, delay:Double(duration) / 1000.0, options:[], animations: { | |
toastLabel.alpha = 0.0; | |
}) { (Bool) in | |
toastLabel.removeFromSuperview(); | |
} | |
} | |
} | |
@IBDesignable class UIPaddingLabel: UILabel { | |
private var _padding:CGFloat = 0.0; | |
public var padding:CGFloat { | |
get { return _padding; } | |
set { | |
_padding = newValue; | |
paddingTop = _padding; | |
paddingLeft = _padding; | |
paddingBottom = _padding; | |
paddingRight = _padding; | |
} | |
} | |
@IBInspectable var paddingTop:CGFloat = 0.0; | |
@IBInspectable var paddingLeft:CGFloat = 0.0; | |
@IBInspectable var paddingBottom:CGFloat = 0.0; | |
@IBInspectable var paddingRight:CGFloat = 0.0; | |
override func drawText(in rect: CGRect) { | |
let insets = UIEdgeInsets(top:paddingTop, left:paddingLeft, bottom:paddingBottom, right:paddingRight); | |
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)); | |
} | |
override var intrinsicContentSize: CGSize { | |
get { | |
var intrinsicSuperViewContentSize = super.intrinsicContentSize; | |
intrinsicSuperViewContentSize.height += paddingTop + paddingBottom; | |
intrinsicSuperViewContentSize.width += paddingLeft + paddingRight; | |
return intrinsicSuperViewContentSize; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment