|
| 1 | +/* |
| 2 | + |
| 3 | + The MIT License (MIT) |
| 4 | + Copyright (c) 2018 Denis Kozhukhov |
| 5 | + |
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 7 | + to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | + and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 9 | + |
| 10 | + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 11 | + |
| 12 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 13 | + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 14 | + ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
| 15 | + THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + |
| 17 | + */ |
| 18 | + |
| 19 | +import UIKit |
| 20 | +import SnapKit |
| 21 | + |
| 22 | +public class FloatGrowingNotificationBanner: GrowingNotificationBanner { |
| 23 | + |
| 24 | + public init(title: String? = nil, |
| 25 | + subtitle: String? = nil, |
| 26 | + titleFont: UIFont? = nil, |
| 27 | + titleColor: UIColor? = nil, |
| 28 | + titleTextAlign: NSTextAlignment? = nil, |
| 29 | + subtitleFont: UIFont? = nil, |
| 30 | + subtitleColor: UIColor? = nil, |
| 31 | + subtitleTextAlign: NSTextAlignment? = nil, |
| 32 | + leftView: UIView? = nil, |
| 33 | + rightView: UIView? = nil, |
| 34 | + style: BannerStyle = .info, |
| 35 | + colors: BannerColorsProtocol? = nil, |
| 36 | + iconPosition: IconPosition = .center) { |
| 37 | + |
| 38 | + super.init(title: title, subtitle: subtitle, leftView: leftView, rightView: rightView, style: style, colors: colors, iconPosition: iconPosition) |
| 39 | + |
| 40 | + if let titleFont = titleFont { |
| 41 | + self.titleFont = titleFont |
| 42 | + titleLabel!.font = titleFont |
| 43 | + } |
| 44 | + |
| 45 | + if let titleColor = titleColor { |
| 46 | + titleLabel!.textColor = titleColor |
| 47 | + } |
| 48 | + |
| 49 | + if let titleTextAlign = titleTextAlign { |
| 50 | + titleLabel!.textAlignment = titleTextAlign |
| 51 | + } |
| 52 | + |
| 53 | + if let subtitleFont = subtitleFont { |
| 54 | + self.subtitleFont = subtitleFont |
| 55 | + subtitleLabel!.font = subtitleFont |
| 56 | + } |
| 57 | + |
| 58 | + if let subtitleColor = subtitleColor { |
| 59 | + subtitleLabel!.textColor = subtitleColor |
| 60 | + } |
| 61 | + |
| 62 | + if let subtitleTextAlign = subtitleTextAlign { |
| 63 | + subtitleLabel!.textAlignment = subtitleTextAlign |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + Convenience function to display banner with non .zero default edge insets |
| 69 | + */ |
| 70 | + public func show(queuePosition: QueuePosition = .back, |
| 71 | + bannerPosition: BannerPosition = .top, |
| 72 | + queue: NotificationBannerQueue = NotificationBannerQueue.default, |
| 73 | + on viewController: UIViewController? = nil, |
| 74 | + edgeInsets: UIEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8), |
| 75 | + cornerRadius: CGFloat? = nil, |
| 76 | + shadowColor: UIColor = .black, |
| 77 | + shadowOpacity: CGFloat = 1, |
| 78 | + shadowBlurRadius: CGFloat = 0, |
| 79 | + shadowCornerRadius: CGFloat = 0, |
| 80 | + shadowOffset: UIOffset = .zero, |
| 81 | + shadowEdgeInsets: UIEdgeInsets? = nil) { |
| 82 | + |
| 83 | + self.bannerEdgeInsets = edgeInsets |
| 84 | + |
| 85 | + if let cornerRadius = cornerRadius { |
| 86 | + contentView.layer.cornerRadius = cornerRadius |
| 87 | + } |
| 88 | + |
| 89 | + show(queuePosition: queuePosition, |
| 90 | + bannerPosition: bannerPosition, |
| 91 | + queue: queue, |
| 92 | + on: viewController) |
| 93 | + |
| 94 | + applyShadow(withColor: shadowColor, |
| 95 | + opacity: shadowOpacity, |
| 96 | + blurRadius: shadowBlurRadius, |
| 97 | + cornerRadius: shadowCornerRadius, |
| 98 | + offset: shadowOffset, |
| 99 | + edgeInsets: shadowEdgeInsets) |
| 100 | + } |
| 101 | + |
| 102 | + required public init?(coder aDecoder: NSCoder) { |
| 103 | + fatalError("init(coder:) has not been implemented") |
| 104 | + } |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +private extension FloatGrowingNotificationBanner { |
| 109 | + |
| 110 | + /** |
| 111 | + Add shadow for notification with specified parameters. |
| 112 | + */ |
| 113 | + private func applyShadow(withColor color: UIColor = .black, |
| 114 | + opacity: CGFloat = 1, |
| 115 | + blurRadius: CGFloat = 0, |
| 116 | + cornerRadius: CGFloat = 0, |
| 117 | + offset: UIOffset = .zero, |
| 118 | + edgeInsets: UIEdgeInsets? = nil) { |
| 119 | + |
| 120 | + contentView.layer.shadowColor = color.cgColor |
| 121 | + contentView.layer.shadowOpacity = Float(opacity) |
| 122 | + contentView.layer.shadowRadius = blurRadius |
| 123 | + contentView.layer.shadowOffset = CGSize(width: offset.horizontal, height: offset.vertical) |
| 124 | + |
| 125 | + if let edgeInsets = edgeInsets { |
| 126 | + var shadowRect = CGRect(origin: .zero, size: bannerPositionFrame.startFrame.size) |
| 127 | + shadowRect.size.height -= (spacerViewHeight() - spacerViewDefaultOffset) // to proper handle spacer height affects |
| 128 | + shadowRect.origin.x += edgeInsets.left |
| 129 | + shadowRect.origin.y += edgeInsets.top |
| 130 | + shadowRect.size.width -= (edgeInsets.left + edgeInsets.right) |
| 131 | + shadowRect.size.height -= (edgeInsets.top + edgeInsets.bottom) |
| 132 | + contentView.layer.shadowPath = UIBezierPath(roundedRect: shadowRect, cornerRadius: cornerRadius).cgPath |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments