PPBadgeControl.swift 4.2 KB

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