IQTitleBarButtonItem.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // IQTitleBarButtonItem.swift
  3. // https://github.com/hackiftekhar/IQKeyboardManager
  4. // Copyright (c) 2013-16 Iftekhar Qurashi.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. import Foundation
  24. import UIKit
  25. open class IQTitleBarButtonItem: IQBarButtonItem {
  26. @objc open var titleFont: UIFont? {
  27. didSet {
  28. if let unwrappedFont = titleFont {
  29. titleButton?.titleLabel?.font = unwrappedFont
  30. } else {
  31. titleButton?.titleLabel?.font = UIFont.systemFont(ofSize: 13)
  32. }
  33. }
  34. }
  35. @objc override open var title: String? {
  36. didSet {
  37. titleButton?.setTitle(title, for: .normal)
  38. }
  39. }
  40. /**
  41. titleColor to be used for displaying button text when displaying title (disabled state).
  42. */
  43. @objc open var titleColor: UIColor? {
  44. didSet {
  45. if let color = titleColor {
  46. titleButton?.setTitleColor(color, for: .disabled)
  47. } else {
  48. titleButton?.setTitleColor(UIColor.lightGray, for: .disabled)
  49. }
  50. }
  51. }
  52. /**
  53. selectableTitleColor to be used for displaying button text when button is enabled.
  54. */
  55. @objc open var selectableTitleColor: UIColor? {
  56. didSet {
  57. if let color = selectableTitleColor {
  58. titleButton?.setTitleColor(color, for: .normal)
  59. } else {
  60. titleButton?.setTitleColor(UIColor.init(red: 0.0, green: 0.5, blue: 1.0, alpha: 1), for: .normal)
  61. }
  62. }
  63. }
  64. /**
  65. Customized Invocation to be called on title button action. titleInvocation is internally created using setTitleTarget:action: method.
  66. */
  67. @objc override open var invocation: IQInvocation? {
  68. didSet {
  69. if let target = invocation?.target, let action = invocation?.action {
  70. self.isEnabled = true
  71. titleButton?.isEnabled = true
  72. titleButton?.addTarget(target, action: action, for: .touchUpInside)
  73. } else {
  74. self.isEnabled = false
  75. titleButton?.isEnabled = false
  76. titleButton?.removeTarget(nil, action: nil, for: .touchUpInside)
  77. }
  78. }
  79. }
  80. internal var titleButton: UIButton?
  81. private var _titleView: UIView?
  82. override init() {
  83. super.init()
  84. }
  85. @objc public convenience init(title: String?) {
  86. self.init(title: nil, style: .plain, target: nil, action: nil)
  87. _titleView = UIView()
  88. _titleView?.backgroundColor = UIColor.clear
  89. titleButton = UIButton(type: .system)
  90. titleButton?.isEnabled = false
  91. titleButton?.titleLabel?.numberOfLines = 3
  92. titleButton?.setTitleColor(UIColor.lightGray, for: .disabled)
  93. titleButton?.setTitleColor(UIColor.init(red: 0.0, green: 0.5, blue: 1.0, alpha: 1), for: .normal)
  94. titleButton?.backgroundColor = UIColor.clear
  95. titleButton?.titleLabel?.textAlignment = .center
  96. titleButton?.setTitle(title, for: .normal)
  97. titleFont = UIFont.systemFont(ofSize: 13.0)
  98. titleButton?.titleLabel?.font = self.titleFont
  99. _titleView?.addSubview(titleButton!)
  100. #if swift(>=3.2)
  101. if #available(iOS 11, *) {
  102. var layoutDefaultLowPriority: UILayoutPriority
  103. var layoutDefaultHighPriority: UILayoutPriority
  104. #if swift(>=4.0)
  105. let layoutPriorityLowValue = UILayoutPriority.defaultLow.rawValue-1
  106. let layoutPriorityHighValue = UILayoutPriority.defaultHigh.rawValue-1
  107. layoutDefaultLowPriority = UILayoutPriority(rawValue: layoutPriorityLowValue)
  108. layoutDefaultHighPriority = UILayoutPriority(rawValue: layoutPriorityHighValue)
  109. #else
  110. layoutDefaultLowPriority = UILayoutPriorityDefaultLow-1
  111. layoutDefaultHighPriority = UILayoutPriorityDefaultHigh-1
  112. #endif
  113. _titleView?.translatesAutoresizingMaskIntoConstraints = false
  114. _titleView?.setContentHuggingPriority(layoutDefaultLowPriority, for: .vertical)
  115. _titleView?.setContentHuggingPriority(layoutDefaultLowPriority, for: .horizontal)
  116. _titleView?.setContentCompressionResistancePriority(layoutDefaultHighPriority, for: .vertical)
  117. _titleView?.setContentCompressionResistancePriority(layoutDefaultHighPriority, for: .horizontal)
  118. titleButton?.translatesAutoresizingMaskIntoConstraints = false
  119. titleButton?.setContentHuggingPriority(layoutDefaultLowPriority, for: .vertical)
  120. titleButton?.setContentHuggingPriority(layoutDefaultLowPriority, for: .horizontal)
  121. titleButton?.setContentCompressionResistancePriority(layoutDefaultHighPriority, for: .vertical)
  122. titleButton?.setContentCompressionResistancePriority(layoutDefaultHighPriority, for: .horizontal)
  123. let top = NSLayoutConstraint.init(item: titleButton!, attribute: .top, relatedBy: .equal, toItem: _titleView, attribute: .top, multiplier: 1, constant: 0)
  124. let bottom = NSLayoutConstraint.init(item: titleButton!, attribute: .bottom, relatedBy: .equal, toItem: _titleView, attribute: .bottom, multiplier: 1, constant: 0)
  125. let leading = NSLayoutConstraint.init(item: titleButton!, attribute: .leading, relatedBy: .equal, toItem: _titleView, attribute: .leading, multiplier: 1, constant: 0)
  126. let trailing = NSLayoutConstraint.init(item: titleButton!, attribute: .trailing, relatedBy: .equal, toItem: _titleView, attribute: .trailing, multiplier: 1, constant: 0)
  127. _titleView?.addConstraints([top, bottom, leading, trailing])
  128. } else {
  129. _titleView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  130. titleButton?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  131. }
  132. #else
  133. _titleView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  134. titleButton?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  135. #endif
  136. customView = _titleView
  137. }
  138. @objc required public init?(coder aDecoder: NSCoder) {
  139. super.init(coder: aDecoder)
  140. }
  141. deinit {
  142. customView = nil
  143. titleButton?.removeTarget(nil, action: nil, for: .touchUpInside)
  144. _titleView = nil
  145. titleButton = nil
  146. }
  147. }