ESTabBarItemBadgeView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // ESTabBarItemBadgeView.swift
  3. //
  4. // Created by Vincent Li on 2017/2/8.
  5. // Copyright (c) 2013-2018 ESTabBarController (https://github.com/eggswift/ESTabBarController)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. import UIKit
  26. /*
  27. * ESTabBarItemBadgeView
  28. * 这个类定义了item中使用的badge视图样式,默认为ESTabBarItemBadgeView类对象。
  29. * 你可以设置ESTabBarItemContentView的badgeView属性为自定义的ESTabBarItemBadgeView子类,这样就可以轻松实现 自定义通知样式了。
  30. */
  31. open class ESTabBarItemBadgeView: UIView {
  32. /// 默认颜色
  33. public static var defaultBadgeColor = UIColor(red: 255.0/255.0, green: 59.0/255.0, blue: 48.0/255.0, alpha: 1.0)
  34. /// Badge color
  35. open var badgeColor: UIColor? = defaultBadgeColor {
  36. didSet {
  37. imageView.backgroundColor = badgeColor
  38. }
  39. }
  40. /// Badge value, supprot nil, "", "1", "someText". Hidden when nil. Show Little dot style when "".
  41. open var badgeValue: String? {
  42. didSet {
  43. badgeLabel.text = badgeValue
  44. }
  45. }
  46. /// Image view
  47. open var imageView: UIImageView = {
  48. let imageView = UIImageView.init(frame: CGRect.zero)
  49. imageView.backgroundColor = .clear
  50. return imageView
  51. }()
  52. /// 显示badgeValue的Label
  53. open var badgeLabel: UILabel = {
  54. let badgeLabel = UILabel.init(frame: CGRect.zero)
  55. badgeLabel.backgroundColor = .clear
  56. badgeLabel.textColor = .white
  57. badgeLabel.font = UIFont.systemFont(ofSize: 13.0)
  58. badgeLabel.textAlignment = .center
  59. return badgeLabel
  60. }()
  61. /// Initializer
  62. public override init(frame: CGRect) {
  63. super.init(frame: frame)
  64. self.addSubview(imageView)
  65. self.addSubview(badgeLabel)
  66. self.imageView.backgroundColor = badgeColor
  67. }
  68. public required init?(coder aDecoder: NSCoder) {
  69. fatalError("init(coder:) has not been implemented")
  70. }
  71. /*
  72. * 通过layoutSubviews()布局子视图,你可以通过重写此方法实现自定义布局。
  73. **/
  74. open override func layoutSubviews() {
  75. super.layoutSubviews()
  76. guard let badgeValue = badgeValue else {
  77. imageView.isHidden = true
  78. badgeLabel.isHidden = true
  79. return
  80. }
  81. imageView.isHidden = false
  82. badgeLabel.isHidden = false
  83. if badgeValue == "" {
  84. imageView.frame = CGRect.init(origin: CGPoint.init(x: (bounds.size.width - 8.0) / 2.0, y: (bounds.size.height - 8.0) / 2.0), size: CGSize.init(width: 8.0, height: 8.0))
  85. } else {
  86. imageView.frame = bounds
  87. }
  88. imageView.layer.cornerRadius = imageView.bounds.size.height / 2.0
  89. badgeLabel.sizeToFit()
  90. badgeLabel.center = imageView.center
  91. }
  92. /*
  93. * 通过此方法计算badge视图需要占用父视图的frame大小,通过重写此方法可以自定义badge视图的大小。
  94. * 如果你需要自定义badge视图在Content中的位置,可以设置Content的badgeOffset属性。
  95. */
  96. open override func sizeThatFits(_ size: CGSize) -> CGSize {
  97. guard let _ = badgeValue else {
  98. return CGSize.init(width: 18.0, height: 18.0)
  99. }
  100. let textSize = badgeLabel.sizeThatFits(CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
  101. return CGSize.init(width: max(18.0, textSize.width + 10.0), height: 18.0)
  102. }
  103. }