IQTitleBarButtonItem.m 6.6 KB

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