NotificationBanner.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2017-2018 Dalton Hinterscher
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. 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:
  7. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  9. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  10. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
  11. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  12. */
  13. import UIKit
  14. import SnapKit
  15. import MarqueeLabel
  16. @objcMembers
  17. open class NotificationBanner: BaseNotificationBanner {
  18. /// The bottom most label of the notification if a subtitle is provided
  19. public private(set) var subtitleLabel: MarqueeLabel?
  20. /// The view that is presented on the left side of the notification
  21. private var leftView: UIView?
  22. /// The view that is presented on the right side of the notification
  23. private var rightView: UIView?
  24. /// Font used for the title label
  25. private var titleFont: UIFont = UIFont.systemFont(ofSize: 17.5, weight: UIFont.Weight.bold)
  26. /// Font used for the subtitle label
  27. private var subtitleFont: UIFont = UIFont.systemFont(ofSize: 15.0)
  28. public init(title: String? = nil,
  29. subtitle: String? = nil,
  30. leftView: UIView? = nil,
  31. rightView: UIView? = nil,
  32. style: BannerStyle = .info,
  33. colors: BannerColorsProtocol? = nil) {
  34. super.init(style: style, colors: colors)
  35. if let leftView = leftView {
  36. contentView.addSubview(leftView)
  37. let size = (leftView.frame.height > 0) ? min(44, leftView.frame.height) : 44
  38. leftView.snp.makeConstraints({ (make) in
  39. make.centerY.equalToSuperview().offset(heightAdjustment / 4)
  40. make.left.equalToSuperview().offset(10)
  41. make.size.equalTo(size)
  42. })
  43. }
  44. if let rightView = rightView {
  45. contentView.addSubview(rightView)
  46. let size = (rightView.frame.height > 0) ? min(44, rightView.frame.height) : 44
  47. rightView.snp.makeConstraints({ (make) in
  48. make.centerY.equalToSuperview().offset(heightAdjustment / 4)
  49. make.left.equalToSuperview().offset(10)
  50. make.size.equalTo(size)
  51. })
  52. }
  53. let labelsView = UIView()
  54. contentView.addSubview(labelsView)
  55. if let title = title {
  56. titleLabel = MarqueeLabel()
  57. (titleLabel as! MarqueeLabel).type = .left
  58. titleLabel!.font = titleFont
  59. titleLabel!.textColor = .white
  60. titleLabel!.text = title
  61. labelsView.addSubview(titleLabel!)
  62. titleLabel!.snp.makeConstraints { (make) in
  63. make.top.equalToSuperview()
  64. make.left.equalToSuperview()
  65. make.right.equalToSuperview()
  66. if let _ = subtitle {
  67. titleLabel!.numberOfLines = 1
  68. } else {
  69. titleLabel!.numberOfLines = 2
  70. }
  71. }
  72. }
  73. if let subtitle = subtitle {
  74. subtitleLabel = MarqueeLabel()
  75. subtitleLabel!.type = .left
  76. subtitleLabel!.font = subtitleFont
  77. subtitleLabel!.numberOfLines = 1
  78. subtitleLabel!.textColor = .white
  79. subtitleLabel!.text = subtitle
  80. labelsView.addSubview(subtitleLabel!)
  81. subtitleLabel!.snp.makeConstraints { (make) in
  82. if title != nil {
  83. make.top.equalTo(titleLabel!.snp.bottom).offset(2.5)
  84. make.left.equalTo(titleLabel!)
  85. make.right.equalTo(titleLabel!)
  86. }
  87. else {
  88. make.top.equalToSuperview()
  89. make.left.equalToSuperview()
  90. make.right.equalToSuperview()
  91. }
  92. }
  93. }
  94. labelsView.snp.makeConstraints { (make) in
  95. make.centerY.equalToSuperview().offset(heightAdjustment / 4)
  96. if let leftView = leftView {
  97. make.left.equalTo(leftView.snp.right).offset(padding)
  98. } else {
  99. make.left.equalToSuperview().offset(padding)
  100. }
  101. if let rightView = rightView {
  102. make.right.equalTo(rightView.snp.left).offset(-padding)
  103. } else {
  104. make.right.equalToSuperview().offset(-padding)
  105. }
  106. if let subtitleLabel = subtitleLabel {
  107. make.bottom.equalTo(subtitleLabel)
  108. } else {
  109. make.bottom.equalTo(titleLabel!)
  110. }
  111. }
  112. updateMarqueeLabelsDurations()
  113. }
  114. public convenience init(attributedTitle: NSAttributedString,
  115. attributedSubtitle: NSAttributedString? = nil,
  116. leftView: UIView? = nil,
  117. rightView: UIView? = nil,
  118. style: BannerStyle = .info,
  119. colors: BannerColorsProtocol? = nil) {
  120. let subtitle: String? = (attributedSubtitle != nil) ? "" : nil
  121. self.init(title: "", subtitle: subtitle, leftView: leftView, rightView: rightView, style: style, colors: colors)
  122. titleLabel!.attributedText = attributedTitle
  123. subtitleLabel?.attributedText = attributedSubtitle
  124. }
  125. public init(customView: UIView) {
  126. super.init(style: .customView)
  127. self.customView = customView
  128. contentView.addSubview(customView)
  129. customView.snp.makeConstraints { (make) in
  130. make.edges.equalTo(contentView)
  131. }
  132. spacerView.backgroundColor = customView.backgroundColor
  133. }
  134. required public init?(coder aDecoder: NSCoder) {
  135. fatalError("init(coder:) has not been implemented")
  136. }
  137. internal override func updateMarqueeLabelsDurations() {
  138. super.updateMarqueeLabelsDurations()
  139. subtitleLabel?.speed = .duration(CGFloat(duration <= 3 ? 0.5 : duration - 3))
  140. }
  141. }
  142. public extension NotificationBanner {
  143. func applyStyling(cornerRadius: CGFloat? = nil,
  144. titleFont: UIFont? = nil,
  145. titleColor: UIColor? = nil,
  146. titleTextAlign: NSTextAlignment? = nil,
  147. subtitleFont: UIFont? = nil,
  148. subtitleColor: UIColor? = nil,
  149. subtitleTextAlign: NSTextAlignment? = nil) {
  150. if let cornerRadius = cornerRadius {
  151. contentView.layer.cornerRadius = cornerRadius
  152. }
  153. if let titleFont = titleFont {
  154. titleLabel!.font = titleFont
  155. }
  156. if let titleColor = titleColor {
  157. titleLabel!.textColor = titleColor
  158. }
  159. if let titleTextAlign = titleTextAlign {
  160. titleLabel!.textAlignment = titleTextAlign
  161. }
  162. if let subtitleFont = subtitleFont {
  163. subtitleLabel!.font = subtitleFont
  164. }
  165. if let subtitleColor = subtitleColor {
  166. subtitleLabel!.textColor = subtitleColor
  167. }
  168. if let subtitleTextAlign = subtitleTextAlign {
  169. subtitleLabel!.textAlignment = subtitleTextAlign
  170. }
  171. if titleFont != nil || subtitleFont != nil {
  172. updateBannerHeight()
  173. }
  174. }
  175. }