IQTitleBarButtonItem.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // IQTitleBarButtonItem.m
  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 <UIKit/UIKit.h>
  24. #import "IQTitleBarButtonItem.h"
  25. #import "IQKeyboardManagerConstants.h"
  26. #import "IQKeyboardManagerConstantsInternal.h"
  27. @interface IQTitleBarButtonItem ()
  28. @property(nullable, nonatomic, strong) UIView *titleView;
  29. @property(nullable, nonatomic, strong) UIButton *titleButton;
  30. @end
  31. @implementation IQTitleBarButtonItem
  32. -(nonnull instancetype)initWithTitle:(nullable NSString *)title
  33. {
  34. self = [super init];
  35. if (self)
  36. {
  37. _titleView = [[UIView alloc] init];
  38. _titleView.backgroundColor = [UIColor clearColor];
  39. _titleButton = [UIButton buttonWithType:UIButtonTypeSystem];
  40. _titleButton.enabled = NO;
  41. _titleButton.titleLabel.numberOfLines = 3;
  42. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  43. if (@available(iOS 13.0, *)) {
  44. [_titleButton setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
  45. } else
  46. #endif
  47. {
  48. [_titleButton setTitleColor:[UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
  49. }
  50. [_titleButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
  51. [_titleButton setBackgroundColor:[UIColor clearColor]];
  52. [_titleButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
  53. [self setTitle:title];
  54. [self setTitleFont:[UIFont systemFontOfSize:13.0]];
  55. [_titleView addSubview:_titleButton];
  56. if (@available(iOS 11.0, *))
  57. {
  58. CGFloat layoutDefaultLowPriority = UILayoutPriorityDefaultLow-1;
  59. CGFloat layoutDefaultHighPriority = UILayoutPriorityDefaultHigh-1;
  60. _titleView.translatesAutoresizingMaskIntoConstraints = NO;
  61. [_titleView setContentHuggingPriority:layoutDefaultLowPriority forAxis:UILayoutConstraintAxisVertical];
  62. [_titleView setContentHuggingPriority:layoutDefaultLowPriority forAxis:UILayoutConstraintAxisHorizontal];
  63. [_titleView setContentCompressionResistancePriority:layoutDefaultHighPriority forAxis:UILayoutConstraintAxisVertical];
  64. [_titleView setContentCompressionResistancePriority:layoutDefaultHighPriority forAxis:UILayoutConstraintAxisHorizontal];
  65. _titleButton.translatesAutoresizingMaskIntoConstraints = NO;
  66. [_titleButton setContentHuggingPriority:layoutDefaultLowPriority forAxis:UILayoutConstraintAxisVertical];
  67. [_titleButton setContentHuggingPriority:layoutDefaultLowPriority forAxis:UILayoutConstraintAxisHorizontal];
  68. [_titleButton setContentCompressionResistancePriority:layoutDefaultHighPriority forAxis:UILayoutConstraintAxisVertical];
  69. [_titleButton setContentCompressionResistancePriority:layoutDefaultHighPriority forAxis:UILayoutConstraintAxisHorizontal];
  70. NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:_titleButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_titleView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
  71. NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:_titleButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_titleView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
  72. NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:_titleButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_titleView attribute:NSLayoutAttributeLeading multiplier:1 constant:0];
  73. NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:_titleButton attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_titleView attribute:NSLayoutAttributeTrailing multiplier:1 constant:0];
  74. [_titleView addConstraints:@[top,bottom,leading,trailing]];
  75. }
  76. else
  77. {
  78. _titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  79. _titleButton.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  80. }
  81. self.customView = _titleView;
  82. }
  83. return self;
  84. }
  85. -(void)setTitleFont:(UIFont *)titleFont
  86. {
  87. _titleFont = titleFont;
  88. if (titleFont)
  89. {
  90. _titleButton.titleLabel.font = titleFont;
  91. }
  92. else
  93. {
  94. _titleButton.titleLabel.font = [UIFont systemFontOfSize:13];
  95. }
  96. }
  97. -(void)setTitle:(NSString *)title
  98. {
  99. [super setTitle:title];
  100. [_titleButton setTitle:title forState:UIControlStateNormal];
  101. }
  102. -(void)setTitleColor:(UIColor*)titleColor
  103. {
  104. _titleColor = titleColor;
  105. [_titleButton setTitleColor:_titleColor?:[UIColor lightGrayColor] forState:UIControlStateDisabled];
  106. }
  107. -(void)setSelectableTitleColor:(UIColor*)selectableTitleColor
  108. {
  109. _selectableTitleColor = selectableTitleColor;
  110. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  111. if (@available(iOS 13.0, *)) {
  112. [_titleButton setTitleColor:_selectableTitleColor?:[UIColor systemBlueColor] forState:UIControlStateNormal];
  113. } else
  114. #endif
  115. {
  116. [_titleButton setTitleColor:_selectableTitleColor?:[UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
  117. }
  118. }
  119. -(void)setInvocation:(NSInvocation *)invocation
  120. {
  121. [super setInvocation:invocation];
  122. if (invocation.target == nil || invocation.selector == NULL)
  123. {
  124. self.enabled = NO;
  125. _titleButton.enabled = NO;
  126. [_titleButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
  127. }
  128. else
  129. {
  130. self.enabled = YES;
  131. _titleButton.enabled = YES;
  132. [_titleButton addTarget:invocation.target action:invocation.selector forControlEvents:UIControlEventTouchUpInside];
  133. }
  134. }
  135. -(void)dealloc
  136. {
  137. self.customView = nil;
  138. [_titleButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
  139. _titleView = nil;
  140. _titleButton = nil;
  141. }
  142. @end