VMKeyboardButton.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Copyright © 2019 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // Parts taken from ish: https://github.com/tbodt/ish/blob/master/app/BarButton.m
  17. // Created by Theodore Dubois on 9/22/18.
  18. // Licensed under GNU General Public License 3.0
  19. #import "VMKeyboardButton.h"
  20. extern UIAccessibilityTraits UIAccessibilityTraitToggle;
  21. @implementation VMKeyboardButton
  22. - (void)setup {
  23. self.layer.cornerRadius = 5;
  24. self.layer.shadowOffset = CGSizeMake(0, 1);
  25. self.layer.shadowOpacity = 0.4;
  26. self.layer.shadowRadius = 0;
  27. self.backgroundColor = self.defaultColor;
  28. if (@available(iOS 13.0, *)) {
  29. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  30. self.keyAppearance = UIKeyboardAppearanceDark;
  31. } else {
  32. self.keyAppearance = UIKeyboardAppearanceLight;
  33. }
  34. } else {
  35. self.keyAppearance = UIKeyboardAppearanceLight;
  36. }
  37. self.accessibilityTraits |= UIAccessibilityTraitKeyboardKey;
  38. if (self.toggleable) {
  39. self.accessibilityTraits |= 0x20000000000000;
  40. }
  41. }
  42. - (void)awakeFromNib {
  43. [super awakeFromNib];
  44. [self setup];
  45. }
  46. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
  47. [super traitCollectionDidChange:previousTraitCollection];
  48. if (@available(iOS 13.0, *)) {
  49. if (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
  50. self.keyAppearance = UIKeyboardAppearanceDark;
  51. } else {
  52. self.keyAppearance = UIKeyboardAppearanceLight;
  53. }
  54. }
  55. }
  56. - (UIColor *)primaryColor {
  57. if (self.keyAppearance == UIKeyboardAppearanceLight)
  58. return UIColor.whiteColor;
  59. else
  60. return [UIColor colorWithRed:1 green:1 blue:1 alpha:77/255.];
  61. }
  62. - (UIColor *)secondaryColor {
  63. if (self.keyAppearance == UIKeyboardAppearanceLight)
  64. return [UIColor colorWithRed:172/255. green:180/255. blue:190/255. alpha:1];
  65. else
  66. return [UIColor colorWithRed:147/255. green:147/255. blue:147/255. alpha:66/255.];
  67. }
  68. - (UIColor *)defaultColor {
  69. if (self.secondary)
  70. return self.secondaryColor;
  71. return self.primaryColor;
  72. }
  73. - (UIColor *)highlightedColor {
  74. if (!self.secondary)
  75. return self.secondaryColor;
  76. return self.primaryColor;
  77. }
  78. - (void)chooseBackground {
  79. if (self.selected || self.highlighted || (self.toggleable && self.toggled)) {
  80. self.backgroundColor = self.highlightedColor;
  81. } else {
  82. [UIView animateWithDuration:0 delay:0.1 options:UIViewAnimationOptionAllowUserInteraction animations:^{
  83. self.backgroundColor = self.defaultColor;
  84. } completion:nil];
  85. }
  86. if (self.keyAppearance == UIKeyboardAppearanceLight) {
  87. self.tintColor = UIColor.blackColor;
  88. } else {
  89. self.tintColor = UIColor.whiteColor;
  90. }
  91. [self setTitleColor:self.tintColor forState:UIControlStateNormal];
  92. }
  93. - (void)setHighlighted:(BOOL)highlighted {
  94. [super setHighlighted:highlighted];
  95. [self chooseBackground];
  96. }
  97. - (void)setToggled:(BOOL)toggled {
  98. _toggled = toggled;
  99. [self chooseBackground];
  100. }
  101. - (void)setKeyAppearance:(UIKeyboardAppearance)keyAppearance {
  102. _keyAppearance = keyAppearance;
  103. [self chooseBackground];
  104. }
  105. - (NSString *)accessibilityValue {
  106. if (self.toggleable) {
  107. return self.selected ? @"1" : @"0";
  108. }
  109. return nil;
  110. }
  111. - (void)prepareForInterfaceBuilder {
  112. [super prepareForInterfaceBuilder];
  113. [self setup];
  114. }
  115. @end