MIBadgeButton.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // MIBadgeButton.swift
  3. // MIBadgeButton
  4. //
  5. // Created by Yosemite on 8/27/14.
  6. // Copyright (c) 2014 Youxel Technology. All rights reserved.
  7. //
  8. import UIKit
  9. public class MIBadgeButton: UIButton {
  10. private var badgeLabel: UILabel
  11. public var badgeString: String? {
  12. didSet {
  13. setupBadgeViewWithString(badgeText: badgeString)
  14. }
  15. }
  16. public var badgeEdgeInsets: UIEdgeInsets? {
  17. didSet {
  18. setupBadgeViewWithString(badgeText: badgeString)
  19. }
  20. }
  21. public var badgeBackgroundColor = UIColor.red {
  22. didSet {
  23. badgeLabel.backgroundColor = badgeBackgroundColor
  24. }
  25. }
  26. public var badgeTextColor = UIColor.white {
  27. didSet {
  28. badgeLabel.textColor = badgeTextColor
  29. }
  30. }
  31. override public init(frame: CGRect) {
  32. badgeLabel = UILabel()
  33. super.init(frame: frame)
  34. // Initialization code
  35. setupBadgeViewWithString(badgeText: "")
  36. }
  37. required public init?(coder aDecoder: NSCoder) {
  38. badgeLabel = UILabel()
  39. super.init(coder: aDecoder)
  40. setupBadgeViewWithString(badgeText: "")
  41. }
  42. public func initWithFrame(frame: CGRect, withBadgeString badgeString: String, withBadgeInsets badgeInsets: UIEdgeInsets) -> AnyObject {
  43. badgeLabel = UILabel()
  44. badgeEdgeInsets = badgeInsets
  45. setupBadgeViewWithString(badgeText: badgeString)
  46. return self
  47. }
  48. private func setupBadgeViewWithString(badgeText: String?) {
  49. badgeLabel.clipsToBounds = true
  50. badgeLabel.text = badgeText
  51. badgeLabel.font = UIFont.systemFont(ofSize: 12)
  52. badgeLabel.textAlignment = .center
  53. badgeLabel.sizeToFit()
  54. let badgeSize = badgeLabel.frame.size
  55. let height = max(20, Double(badgeSize.height) + 5.0)
  56. let width = max(height, Double(badgeSize.width) + 10.0)
  57. var vertical: Double?, horizontal: Double?
  58. if let badgeInset = self.badgeEdgeInsets {
  59. vertical = Double(badgeInset.top) - Double(badgeInset.bottom)
  60. horizontal = Double(badgeInset.left) - Double(badgeInset.right)
  61. let x = (Double(bounds.size.width) - 10 + horizontal!)
  62. let y = -(Double(badgeSize.height) / 2) - 10 + vertical!
  63. badgeLabel.frame = CGRect(x: x, y: y, width: width, height: height)
  64. } else {
  65. let x = self.frame.width - CGFloat((width / 2.0))
  66. let y = CGFloat(-(height / 2.0))
  67. badgeLabel.frame = CGRect(x: x, y: y, width: CGFloat(width), height: CGFloat(height))
  68. }
  69. setupBadgeStyle()
  70. addSubview(badgeLabel)
  71. if let text = badgeText {
  72. badgeLabel.isHidden = text != "" ? false : true
  73. } else {
  74. badgeLabel.isHidden = true
  75. }
  76. }
  77. private func setupBadgeStyle() {
  78. badgeLabel.textAlignment = .center
  79. badgeLabel.backgroundColor = badgeBackgroundColor
  80. badgeLabel.textColor = badgeTextColor
  81. badgeLabel.layer.cornerRadius = badgeLabel.bounds.size.height / 2
  82. }
  83. }