FLEXKBToolbarButton.m 5.1 KB

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