TBToolbarButton.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // FLEXToolbarButton.m
  3. //
  4. // Created by Tanner on 6/11/17.
  5. //
  6. #import "TBToolbarButton.h"
  7. #import "UIFont+FLEX.h"
  8. #import "FLEXUtility.h"
  9. #import "CALayer+FLEX.h"
  10. @interface TBToolbarButton ()
  11. @property (nonatomic ) NSString *title;
  12. @property (nonatomic, copy) TBToolbarAction buttonPressBlock;
  13. /// YES if appearance is set to `default`
  14. @property (nonatomic, readonly) BOOL useSystemAppearance;
  15. /// YES if the current trait collection is set to dark mode and \c useSystemAppearance is YES
  16. @property (nonatomic, readonly) BOOL usingDarkMode;
  17. @end
  18. @implementation TBToolbarButton
  19. + (instancetype)buttonWithTitle:(NSString *)title {
  20. return [[self alloc] initWithTitle:title];
  21. }
  22. + (instancetype)buttonWithTitle:(NSString *)title action:(TBToolbarAction)eventHandler forControlEvents:(UIControlEvents)controlEvent {
  23. TBToolbarButton *newButton = [self buttonWithTitle:title];
  24. [newButton addEventHandler:eventHandler forControlEvents:controlEvent];
  25. return newButton;
  26. }
  27. + (instancetype)buttonWithTitle:(NSString *)title action:(TBToolbarAction)eventHandler {
  28. return [self buttonWithTitle:title action:eventHandler forControlEvents:UIControlEventTouchUpInside];
  29. }
  30. - (id)initWithTitle:(NSString *)title {
  31. self = [super init];
  32. if (self) {
  33. _title = title;
  34. self.layer.shadowOffset = CGSizeMake(0, 1);
  35. self.layer.shadowOpacity = 0.35;
  36. self.layer.shadowRadius = 0;
  37. self.layer.cornerRadius = 5;
  38. self.clipsToBounds = NO;
  39. self.titleLabel.font = [UIFont systemFontOfSize:18.0];
  40. self.layer.flex_continuousCorners = YES;
  41. [self setTitle:self.title forState:UIControlStateNormal];
  42. [self sizeToFit];
  43. if (@available(iOS 13, *)) {
  44. self.appearance = UIKeyboardTypeDefault;
  45. } else {
  46. self.appearance = UIKeyboardAppearanceLight;
  47. }
  48. CGRect frame = self.frame;
  49. frame.size.width += title.length < 3 ? 30 : 15;
  50. frame.size.height += 10;
  51. self.frame = frame;
  52. }
  53. return self;
  54. }
  55. - (void)addEventHandler:(TBToolbarAction)eventHandler forControlEvents:(UIControlEvents)controlEvent {
  56. self.buttonPressBlock = eventHandler;
  57. [self addTarget:self action:@selector(buttonPressed) forControlEvents:controlEvent];
  58. }
  59. - (void)buttonPressed {
  60. self.buttonPressBlock(self.title, NO);
  61. }
  62. - (void)setAppearance:(UIKeyboardAppearance)appearance {
  63. _appearance = appearance;
  64. UIColor *titleColor = nil, *backgroundColor = nil;
  65. UIColor *lightColor = [UIColor colorWithRed:253.0/255.0 green:253.0/255.0 blue:254.0/255.0 alpha:1];
  66. UIColor *darkColor = [UIColor colorWithRed:101.0/255.0 green:102.0/255.0 blue:104.0/255.0 alpha:1];
  67. switch (_appearance) {
  68. default:
  69. case UIKeyboardAppearanceDefault:
  70. #if FLEX_AT_LEAST_IOS13_SDK
  71. if (@available(iOS 13, *)) {
  72. titleColor = UIColor.labelColor;
  73. if (self.usingDarkMode) {
  74. // style = UIBlurEffectStyleSystemUltraThinMaterialLight;
  75. backgroundColor = darkColor;
  76. } else {
  77. // style = UIBlurEffectStyleSystemMaterialLight;
  78. backgroundColor = lightColor;
  79. }
  80. break;
  81. }
  82. #endif
  83. case UIKeyboardAppearanceLight:
  84. titleColor = UIColor.blackColor;
  85. backgroundColor = lightColor;
  86. // style = UIBlurEffectStyleExtraLight;
  87. break;
  88. case UIKeyboardAppearanceDark:
  89. titleColor = UIColor.whiteColor;
  90. backgroundColor = darkColor;
  91. // style = UIBlurEffectStyleDark;
  92. break;
  93. }
  94. self.backgroundColor = backgroundColor;
  95. [self setTitleColor:titleColor forState:UIControlStateNormal];
  96. }
  97. - (BOOL)isEqual:(id)object {
  98. if ([object isKindOfClass:[TBToolbarButton class]]) {
  99. return [self.title isEqualToString:[object title]];
  100. }
  101. return NO;
  102. }
  103. - (NSUInteger)hash {
  104. return self.title.hash;
  105. }
  106. - (BOOL)useSystemAppearance {
  107. return self.appearance == UIKeyboardAppearanceDefault;
  108. }
  109. - (BOOL)usingDarkMode {
  110. if (@available(iOS 12, *)) {
  111. return self.useSystemAppearance && self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark;
  112. }
  113. return self.appearance == UIKeyboardAppearanceDark;
  114. }
  115. - (void)traitCollectionDidChange:(UITraitCollection *)previous {
  116. if (@available(iOS 12, *)) {
  117. // Was darkmode toggled?
  118. if (previous.userInterfaceStyle != self.traitCollection.userInterfaceStyle) {
  119. if (self.useSystemAppearance) {
  120. // Recreate the background view with the proper colors
  121. self.appearance = self.appearance;
  122. }
  123. }
  124. }
  125. }
  126. @end
  127. @implementation TBToolbarSuggestedButton
  128. - (void)buttonPressed {
  129. self.buttonPressBlock(self.title, YES);
  130. }
  131. @end