SAIInputItem.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // SAIInputItem.swift
  3. // SAIInputBar
  4. //
  5. // Created by SAGESSE on 8/3/16.
  6. // Copyright © 2016-2017 SAGESSE. All rights reserved.
  7. //
  8. import UIKit
  9. public enum SAIInputItemPosition: Int {
  10. case top = 0
  11. case left = 1
  12. case right = 3
  13. case bottom = 4
  14. case center = 2
  15. }
  16. public enum SAIInputItemAlignment: Int {
  17. //0xvvhh
  18. case top = 0x0104 // Top + Center(H)
  19. case bottom = 0x0204 // Bottom + Center(H)
  20. case left = 0x0401 // Center(V) + Left
  21. case right = 0x0402 // Center(V) + Right
  22. case topLeft = 0x0101 // Top + Left
  23. case topRight = 0x0102 // Top + Right
  24. case bottomLeft = 0x0201 // Bottom + Left
  25. case bottomRight = 0x0202 // Bottom + Right
  26. case center = 0x0404 // Center(V) + Center(H)
  27. case automatic = 0x0000
  28. }
  29. open class SAIInputItem: NSObject {
  30. // MARK: property
  31. open lazy var identifier: String = UUID().uuidString
  32. open var size: CGSize = CGSize.zero // default is CGSizeZero
  33. open var image: UIImage? // default is nil
  34. open var customView: UIView? // default is nil
  35. open var tag: Int = 0 // default is 0
  36. open var title: String? // default is nil
  37. open var enabled: Bool = true // default is YES
  38. open var font: UIFont? // default is nil
  39. open var backgroundColor: UIColor? // default is nil
  40. open var handler: ((SAIInputItem) -> Void)? // default is nil
  41. open var tintColor: UIColor?
  42. open var alignment: SAIInputItemAlignment = .automatic
  43. open var imageInsets: UIEdgeInsets = .zero // default is UIEdgeInsetsZero
  44. // MARK: setter
  45. open func setTitle(_ title: String?, for state: UIControl.State) {
  46. _titles[state.rawValue] = title
  47. }
  48. open func setTitleColor(_ color: UIColor?, for state: UIControl.State) {
  49. _titleColors[state.rawValue] = color
  50. }
  51. open func setTitleShadowColor(_ color: UIColor?, for state: UIControl.State) {
  52. _titleShadowColors[state.rawValue] = color
  53. }
  54. open func setAttributedTitle(_ title: NSAttributedString?, for state: UIControl.State) {
  55. _attributedTitles[state.rawValue] = title
  56. }
  57. open func setImage(_ image: UIImage?, for state: UIControl.State) {
  58. _images[state.rawValue] = image
  59. }
  60. open func setBackgroundImage(_ image: UIImage?, for state: UIControl.State) {
  61. _backgroundImages[state.rawValue] = image
  62. }
  63. // MARK: getter
  64. open func title(for state: UIControl.State) -> String? {
  65. return _titles[state.rawValue] ?? nil
  66. }
  67. open func titleColor(for state: UIControl.State) -> UIColor? {
  68. return _titleColors[state.rawValue] ?? nil
  69. }
  70. open func titleShadowColor(for state: UIControl.State) -> UIColor? {
  71. return _titleShadowColors[state.rawValue] ?? nil
  72. }
  73. open func attributedTitle(for state: UIControl.State) -> NSAttributedString? {
  74. return _attributedTitles[state.rawValue] ?? nil
  75. }
  76. open func image(for state: UIControl.State) -> UIImage? {
  77. return _images[state.rawValue] ?? nil
  78. }
  79. open func backgroundImage(for state: UIControl.State) -> UIImage? {
  80. return _backgroundImages[state.rawValue] ?? nil
  81. }
  82. open override var hash: Int {
  83. return identifier.hash
  84. }
  85. open override var hashValue: Int {
  86. return identifier.hashValue
  87. }
  88. // MARK: ivar
  89. private var _titles: [UInt: String?] = [:]
  90. private var _titleColors: [UInt: UIColor?] = [:]
  91. private var _titleShadowColors: [UInt: UIColor?] = [:]
  92. private var _attributedTitles: [UInt: NSAttributedString?] = [:]
  93. private var _images: [UInt: UIImage?] = [:]
  94. private var _backgroundImages: [UInt: UIImage?] = [:]
  95. // MARK: create
  96. public override init() {
  97. super.init()
  98. }
  99. public convenience init(image: UIImage?, handler: ((SAIInputItem) -> Void)? = nil) {
  100. self.init()
  101. self.image = image
  102. self.handler = handler
  103. }
  104. public convenience init(title: String?, handler: ((SAIInputItem) -> Void)? = nil) {
  105. self.init()
  106. self.title = title
  107. self.handler = handler
  108. }
  109. public convenience init(customView: UIView) {
  110. self.init()
  111. self.customView = customView
  112. }
  113. }
  114. extension SAIInputItemPosition: CustomStringConvertible {
  115. public var description: String {
  116. switch self {
  117. case .top: return "Top(\(rawValue))"
  118. case .left: return "Left(\(rawValue))"
  119. case .right: return "Right(\(rawValue))"
  120. case .bottom: return "Bottom(\(rawValue))"
  121. case .center: return "Center(\(rawValue))"
  122. }
  123. }
  124. }