123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- import UIKit
- class FFToastView: UIView {
-
- var text: String = "" {
- didSet {
- label.text = text
- label.numberOfLines = 0
- label.lineBreakMode = .byWordWrapping
- label.textAlignment = .center
- label.sizeToFit()
- }
- }
-
- var textFont = UIFont.systemFont(ofSize: 14)
- var textColor = UIColor.white
-
- lazy var activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
-
- var centerXPadding: CGFloat = 0
-
- var centerXMultiplier: CGFloat = 1
-
- var centerYPadding: CGFloat = 0
- var centerYMultiplier: CGFloat = 1
-
- var labelEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) {
- didSet {
- if !UIEdgeInsetsEqualToEdgeInsets(labelEdgeInsets, oldValue) {
- self.setNeedsUpdateConstraints()
- }
- }
- }
-
- var viewCornerRadius: CGFloat = 5 {
- didSet {
- self.layer.masksToBounds = true
- self.layer.cornerRadius = viewCornerRadius
- }
- }
- fileprivate var showing = false
-
- func showToast(inView superview: UIView, duration: TimeInterval) {
- showing = true
- self.superview?.isUserInteractionEnabled = true
- label.removeFromSuperview()
- self.addToSuperView(superview, duration: duration, haveImage: true)
- self.addSubview(label)
- activityIndicatorView.removeFromSuperview()
- addLabelConstraints()
- }
- func showImageToast(inView superview: UIView, duration: TimeInterval) {
- showing = true
- self.superview?.isUserInteractionEnabled = true
- label.removeFromSuperview()
- imageView.removeFromSuperview()
- self.addToSuperView(superview, duration: duration)
- self.addSubview(label)
- self.addSubview(imageView)
- activityIndicatorView.removeFromSuperview()
- addImageViewConstraints()
- }
-
- func showLoadingToast(inView superview: UIView, blockSuperView: Bool) {
- showing = true
- self.superview?.isUserInteractionEnabled = true
- label.removeFromSuperview()
- self.addAcitvityIndicatorAndAddConstraints()
- activityIndicatorView.startAnimating()
- if blockSuperView {
- superview.isUserInteractionEnabled = false
- }
- self.addToSuperView(superview, duration: 0)
- }
- func hideLoadingToast() {
- showing = false
- activityIndicatorView.stopAnimating()
- activityIndicatorView.removeFromSuperview()
- self.dismiss()
- }
- func addToSuperView(_ superview: UIView, duration: TimeInterval, haveImage: Bool = false) {
- self.removeFromSuperview()
- superview.addSubview(self)
- self.layer.removeAllAnimations()
- self.alpha = 1
-
- self.constraintCenterX = NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: superview, attribute: .centerX, multiplier: centerXMultiplier, constant: centerXPadding)
- self.constraintCenterY = NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: superview, attribute: .centerY, multiplier: centerYMultiplier, constant: centerYPadding)
- if self.bounds.width == 0 {
- self.superview?.layoutIfNeeded()
- }
- self.layoutIfNeeded()
- if duration > 0 {
- self.perform(#selector(FFToastView.dismiss), with: nil, afterDelay: duration)
- }
- }
- @objc func dismiss() {
- showing = false
- UIView.animate(withDuration: 0.2, animations: {
- self.alpha = 0
- }, completion: { (_) in
- self.superview?.isUserInteractionEnabled = true
- if !self.showing {
- self.removeFromSuperview()
- }
- self.alpha = 1
- })
- }
- @discardableResult
- class func showToast(inView superview: UIView, withText text: String) -> FFToastView {
- defaultInstance.text = text
- defaultInstance.showToast(inView: superview, duration: 2)
- return defaultInstance
- }
- @discardableResult
- class func showToastAndTime(inView superview: UIView, withText text: String, withDuration duration: TimeInterval) -> FFToastView {
- defaultInstance.text = text
- defaultInstance.showToast(inView: superview, duration: duration)
- return defaultInstance
- }
- @discardableResult
- class func showImageToast(inView superview: UIView, withText text: String, withImage image: String) -> FFToastView {
- defaultInstance.text = text
- defaultInstance.imageView.image = UIImage(named: image)
- defaultInstance.showImageToast(inView: superview, duration: 2)
- return defaultInstance
- }
- class func hideLoadingToast() {
- defaultInstance.hideLoadingToast()
- }
- @discardableResult
- class func showLoadingToast(inView superview: UIView, blockSuperView block: Bool) -> FFToastView {
- defaultInstance.showLoadingToast(inView: superview, blockSuperView : block)
- return defaultInstance
- }
-
- static fileprivate let defaultInstance = FFToastView(frame: CGRect.zero)
- fileprivate let label = UILabel()
- fileprivate let imageView = UIImageView()
- fileprivate var constraintCenterX: NSLayoutConstraint?
- fileprivate var constraintCenterY: NSLayoutConstraint?
- fileprivate var innerConstraints: [NSLayoutConstraint] = []
- fileprivate func applyDefaultSetting() {
- self.backgroundColor = UIColor(white: 0, alpha: 0.8)
- label.textColor = self.textColor
- label.font = self.textFont
- self.viewCornerRadius = 5
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- self.applyDefaultSetting()
- }
- override init(frame: CGRect) {
- super.init(frame: frame)
- self.applyDefaultSetting()
- }
- override func didMoveToSuperview() {
- if superview == nil {
- self.constraintCenterX = nil
- self.constraintCenterY = nil
- }
- }
- func addImageViewConstraints() {
- self.removeConstraints(constraints)
- if imageView.superview == self && label.superview == self {
- let rect = NSString(string: text).boundingRect(with: CGSize(width: kScreenWidth - 50, height: 10000), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)], context: nil)
- let size = NSString(string: text).size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)])
- let width = size.width < kScreenWidth - 50 ? size.width : kScreenWidth - 50
- imageView.frame = CGRect(x: 34, y: 18, width: 60, height: 60)
- label.frame = CGRect(x: 10, y: 90, width: width, height: rect.height)
- innerConstraints.removeAll(keepingCapacity: true)
- }
- }
- func addLabelConstraints() {
- imageView.removeFromSuperview()
- self.removeConstraints(constraints)
- let rect = NSString(string: text).boundingRect(with: CGSize(width: kScreenWidth - 50, height: 10000), options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15)], context: nil)
- let size = NSString(string: text).size(withAttributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14)])
- let width = size.width < kScreenWidth - 50 ? size.width : kScreenWidth - 50
- label.frame = CGRect(x: 10, y: 10, width: width, height: rect.height)
- }
- fileprivate func addAcitvityIndicatorAndAddConstraints() {
- imageView.removeFromSuperview()
- removeConstraints(constraints)
- if activityIndicatorView.superview != self {
- self.addSubview(activityIndicatorView)
- innerConstraints.removeAll()
- }
- }
- }
|