NXBadgeControl.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // NXBadgeControl.swift
  3. // NXFramework-Swift
  4. //
  5. // Created by ak on 2020/11/11.
  6. //
  7. import UIKit
  8. open class NXBadgeControl: UIControl {
  9. /// 记录Badge的偏移量 Record the offset of Badge
  10. public var offset: CGPoint = CGPoint(x: 0, y: 0)
  11. /// Badge伸缩的方向, Default is NXBadgeViewFlexModeTail
  12. public var flexMode: NXBadgeViewFlexMode = .tail
  13. private lazy var textLabel: UILabel = UILabel()
  14. private lazy var imageView: UIImageView = UIImageView()
  15. private var badgeViewColor: UIColor?
  16. private var badgeViewHeightConstraint: NSLayoutConstraint?
  17. public class func `default`() -> Self {
  18. return self.init(frame: .zero)
  19. }
  20. required override public init(frame: CGRect) {
  21. super.init(frame: frame)
  22. setupSubviews()
  23. }
  24. required public init?(coder aDecoder: NSCoder) {
  25. super.init(coder: aDecoder)
  26. fatalError("init(coder:) has not been implemented")
  27. }
  28. /// Set Text
  29. open var text: String? {
  30. didSet {
  31. textLabel.text = text
  32. }
  33. }
  34. /// Set AttributedText
  35. open var attributedText: NSAttributedString? {
  36. didSet {
  37. textLabel.attributedText = attributedText
  38. }
  39. }
  40. /// Set Font
  41. open var font: UIFont? {
  42. didSet {
  43. textLabel.font = font
  44. }
  45. }
  46. /// Set background image
  47. open var backgroundImage: UIImage? {
  48. didSet {
  49. imageView.image = backgroundImage
  50. if let _ = backgroundImage {
  51. if let constraint = heightConstraint() {
  52. badgeViewHeightConstraint = constraint
  53. removeConstraint(constraint)
  54. }
  55. backgroundColor = UIColor.clear
  56. } else {
  57. if heightConstraint() == nil, let constraint = badgeViewHeightConstraint {
  58. addConstraint(constraint)
  59. }
  60. backgroundColor = badgeViewColor
  61. }
  62. }
  63. }
  64. open override var backgroundColor: UIColor? {
  65. didSet {
  66. super.backgroundColor = backgroundColor
  67. if let color = backgroundColor, color != .clear {
  68. badgeViewColor = backgroundColor
  69. }
  70. }
  71. }
  72. private func setupSubviews() {
  73. layer.masksToBounds = true
  74. layer.cornerRadius = 9.0
  75. translatesAutoresizingMaskIntoConstraints = false
  76. backgroundColor = UIColor.red
  77. textLabel.textColor = UIColor.white
  78. textLabel.font = UIFont.systemFont(ofSize: 13)
  79. textLabel.textAlignment = .center
  80. addSubview(textLabel)
  81. addSubview(imageView)
  82. addLayout(with: imageView, leading: 0, trailing: 0)
  83. addLayout(with: textLabel, leading: 5, trailing: -5)
  84. }
  85. private func addLayout(with view: UIView, leading: CGFloat, trailing: CGFloat) {
  86. view.translatesAutoresizingMaskIntoConstraints = false
  87. let topConstraint = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0)
  88. let leadingConstraint = NSLayoutConstraint(item: view, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: leading)
  89. let bottomConstraint = NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0)
  90. let trailingConstraint = NSLayoutConstraint(item: view, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: trailing)
  91. leadingConstraint.priority = UILayoutPriority(rawValue: 999)
  92. trailingConstraint.priority = UILayoutPriority(rawValue: 999)
  93. addConstraints([topConstraint, leadingConstraint, bottomConstraint, trailingConstraint])
  94. }
  95. }