悟空记账

IQTitleBarButtonItem.m 6.3KB

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