TBToolbarButton.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. @interface TBToolbarButton ()
  10. @property (nonatomic ) NSString *title;
  11. @property (nonatomic, copy) TBToolbarAction buttonPressBlock;
  12. @property (nonatomic ) UIView *backgroundView;
  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 = [TBToolbarButton 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.25f;
  36. self.layer.shadowRadius = 0.f;
  37. self.layer.cornerRadius = 5.f;
  38. self.layer.borderWidth = 1.f;
  39. self.clipsToBounds = NO;
  40. self.titleLabel.font = [UIFont flex_codeFont];
  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 += 40;
  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);
  61. }
  62. - (void)setAppearance:(UIKeyboardAppearance)appearance {
  63. _appearance = appearance;
  64. if (self.backgroundView.superview) {
  65. [self.backgroundView removeFromSuperview];
  66. }
  67. UIColor *titleColor = nil, *borderColor = nil;
  68. UIBlurEffectStyle style;
  69. switch (_appearance) {
  70. default:
  71. case UIKeyboardAppearanceDefault:
  72. #if FLEX_AT_LEAST_IOS13_SDK
  73. if (@available(iOS 13, *)) {
  74. borderColor = [UIColor clearColor];
  75. titleColor = [UIColor labelColor];
  76. if (self.usingDarkMode) {
  77. style = UIBlurEffectStyleSystemUltraThinMaterialLight;
  78. } else {
  79. style = UIBlurEffectStyleSystemMaterialLight;
  80. }
  81. break;
  82. }
  83. #endif
  84. case UIKeyboardAppearanceLight:
  85. borderColor = [UIColor colorWithWhite:1.000 alpha:0.500];
  86. titleColor = [UIColor blackColor];
  87. style = UIBlurEffectStyleRegular;
  88. break;
  89. case UIKeyboardAppearanceDark:
  90. borderColor = [UIColor clearColor];
  91. titleColor = [UIColor whiteColor];
  92. style = UIBlurEffectStyleDark;
  93. break;
  94. }
  95. self.layer.borderColor = borderColor.CGColor;
  96. [self setTitleColor:titleColor forState:UIControlStateNormal];
  97. UIVisualEffect *blur = [UIBlurEffect effectWithStyle:style];
  98. self.backgroundView = [[UIVisualEffectView alloc] initWithEffect:blur];
  99. self.backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  100. self.backgroundView.layer.cornerRadius = self.layer.cornerRadius;
  101. self.backgroundView.clipsToBounds = YES;
  102. // Without these, the background view blocks the button's touches
  103. self.backgroundView.userInteractionEnabled = NO;
  104. self.backgroundView.exclusiveTouch = NO;
  105. [self insertSubview:self.backgroundView atIndex:0];
  106. self.backgroundView.frame = self.bounds;
  107. }
  108. - (BOOL)isEqual:(id)object {
  109. if ([object isKindOfClass:[TBToolbarButton class]]) {
  110. return [self.title isEqualToString:[object title]];
  111. }
  112. return NO;
  113. }
  114. - (NSUInteger)hash {
  115. return self.title.hash;
  116. }
  117. - (BOOL)useSystemAppearance {
  118. return self.appearance == UIKeyboardAppearanceDefault;
  119. }
  120. - (BOOL)usingDarkMode {
  121. if (@available(iOS 12, *)) {
  122. return self.useSystemAppearance && self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark;
  123. }
  124. return self.appearance == UIKeyboardAppearanceDark;
  125. }
  126. - (void)traitCollectionDidChange:(UITraitCollection *)previous {
  127. if (@available(iOS 12, *)) {
  128. // Was darkmode toggled?
  129. if (previous.userInterfaceStyle != self.traitCollection.userInterfaceStyle) {
  130. if (self.useSystemAppearance) {
  131. // Recreate the background view with the proper colors
  132. self.appearance = self.appearance;
  133. }
  134. }
  135. }
  136. }
  137. @end