FLEXKBToolbarButton.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. return [self buttonWithTitle:title action:eventHandler forControlEvents:UIControlEventTouchUpInside];
  31. }
  32. - (id)initWithTitle:(NSString *)title {
  33. self = [super init];
  34. if (self) {
  35. _title = title;
  36. self.layer.shadowOffset = CGSizeMake(0, 1);
  37. self.layer.shadowOpacity = 0.35;
  38. self.layer.shadowRadius = 0;
  39. self.layer.cornerRadius = 5;
  40. self.clipsToBounds = NO;
  41. self.titleLabel.font = [UIFont systemFontOfSize:18.0];
  42. self.layer.flex_continuousCorners = YES;
  43. [self setTitle:self.title forState:UIControlStateNormal];
  44. [self sizeToFit];
  45. if (@available(iOS 13, *)) {
  46. self.appearance = UIKeyboardTypeDefault;
  47. } else {
  48. self.appearance = UIKeyboardAppearanceLight;
  49. }
  50. CGRect frame = self.frame;
  51. frame.size.width += title.length < 3 ? 30 : 15;
  52. frame.size.height += 10;
  53. self.frame = frame;
  54. }
  55. return self;
  56. }
  57. - (void)addEventHandler:(FLEXKBToolbarAction)eventHandler forControlEvents:(UIControlEvents)controlEvent {
  58. self.buttonPressBlock = eventHandler;
  59. [self addTarget:self action:@selector(buttonPressed) forControlEvents:controlEvent];
  60. }
  61. - (void)buttonPressed {
  62. self.buttonPressBlock(self.title, NO);
  63. }
  64. - (void)setAppearance:(UIKeyboardAppearance)appearance {
  65. _appearance = appearance;
  66. UIColor *titleColor = nil, *backgroundColor = nil;
  67. UIColor *lightColor = [UIColor colorWithRed:253.0/255.0 green:253.0/255.0 blue:254.0/255.0 alpha:1];
  68. UIColor *darkColor = [UIColor colorWithRed:101.0/255.0 green:102.0/255.0 blue:104.0/255.0 alpha:1];
  69. switch (_appearance) {
  70. default:
  71. case UIKeyboardAppearanceDefault:
  72. #if FLEX_AT_LEAST_IOS13_SDK
  73. if (@available(iOS 13, *)) {
  74. titleColor = UIColor.labelColor;
  75. if (self.usingDarkMode) {
  76. // style = UIBlurEffectStyleSystemUltraThinMaterialLight;
  77. backgroundColor = darkColor;
  78. } else {
  79. // style = UIBlurEffectStyleSystemMaterialLight;
  80. backgroundColor = lightColor;
  81. }
  82. break;
  83. }
  84. #endif
  85. case UIKeyboardAppearanceLight:
  86. titleColor = UIColor.blackColor;
  87. backgroundColor = lightColor;
  88. // style = UIBlurEffectStyleExtraLight;
  89. break;
  90. case UIKeyboardAppearanceDark:
  91. titleColor = UIColor.whiteColor;
  92. backgroundColor = darkColor;
  93. // style = UIBlurEffectStyleDark;
  94. break;
  95. }
  96. self.backgroundColor = backgroundColor;
  97. [self setTitleColor:titleColor forState:UIControlStateNormal];
  98. }
  99. - (BOOL)isEqual:(id)object {
  100. if ([object isKindOfClass:[FLEXKBToolbarButton class]]) {
  101. return [self.title isEqualToString:[object title]];
  102. }
  103. return NO;
  104. }
  105. - (NSUInteger)hash {
  106. return self.title.hash;
  107. }
  108. - (BOOL)useSystemAppearance {
  109. return self.appearance == UIKeyboardAppearanceDefault;
  110. }
  111. - (BOOL)usingDarkMode {
  112. if (@available(iOS 12, *)) {
  113. return self.useSystemAppearance && self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark;
  114. }
  115. return self.appearance == UIKeyboardAppearanceDark;
  116. }
  117. - (void)traitCollectionDidChange:(UITraitCollection *)previous {
  118. if (@available(iOS 12, *)) {
  119. // Was darkmode toggled?
  120. if (previous.userInterfaceStyle != self.traitCollection.userInterfaceStyle) {
  121. if (self.useSystemAppearance) {
  122. // Recreate the background view with the proper colors
  123. self.appearance = self.appearance;
  124. }
  125. }
  126. }
  127. }
  128. @end
  129. @implementation FLEXKBToolbarSuggestedButton
  130. - (void)buttonPressed {
  131. self.buttonPressBlock(self.title, YES);
  132. }
  133. @end