Files
keyboard/CustomKeyboard/View/KBKeyButton.m

159 lines
5.8 KiB
Mathematica
Raw Normal View History

2025-10-28 10:18:10 +08:00
//
// KBKeyButton.m
// CustomKeyboard
//
#import "KBKeyButton.h"
#import "KBKey.h"
2025-11-04 21:01:46 +08:00
#import "KBSkinManager.h"
2025-10-28 10:18:10 +08:00
2025-11-18 20:53:47 +08:00
@interface KBKeyButton ()
2025-11-20 21:11:27 +08:00
// 便 KBKeyboardView
@property (nonatomic, weak, readonly) UIView *kb_keyboardContainer;
2025-11-18 20:53:47 +08:00
@end
2025-10-28 10:18:10 +08:00
@implementation KBKeyButton
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self applyDefaultStyle];
}
return self;
}
- (void)applyDefaultStyle {
2025-11-04 21:01:46 +08:00
self.titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
KBSkinTheme *t = [KBSkinManager shared].current;
[self setTitleColor:t.keyTextColor forState:UIControlStateNormal];
[self setTitleColor:t.keyTextColor forState:UIControlStateHighlighted];
self.backgroundColor = t.keyBackground;
2025-10-28 10:18:10 +08:00
self.layer.cornerRadius = 6.0; //
self.layer.masksToBounds = NO;
self.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.1].CGColor; //
self.layer.shadowOpacity = 1.0;
self.layer.shadowOffset = CGSizeMake(0, 1);
self.layer.shadowRadius = 1.5;
2025-10-28 20:11:40 +08:00
[self refreshStateAppearance];
2025-11-18 20:53:47 +08:00
//
if (!self.iconView) {
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectZero];
//
2025-11-20 19:57:11 +08:00
iv.contentMode = UIViewContentModeScaleToFill;
2025-11-18 20:53:47 +08:00
iv.clipsToBounds = YES;
iv.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:iv];
//
[NSLayoutConstraint activateConstraints:@[
[iv.topAnchor constraintEqualToAnchor:self.topAnchor],
[iv.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
[iv.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[iv.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
]];
self.iconView = iv;
//
self.titleEdgeInsets = UIEdgeInsetsZero;
[self bringSubviewToFront:self.titleLabel];
}
}
- (void)setKey:(KBKey *)key {
_key = key;
2025-10-28 10:18:10 +08:00
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
2025-11-20 20:36:31 +08:00
//
//
CGFloat scale = highlighted ? 0.9 : 1.0; // 0.9~0.95
[UIView animateWithDuration:0.08
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseOut
animations:^{
self.transform = CGAffineTransformMakeScale(scale, scale);
}
completion:nil];
2025-11-20 21:11:27 +08:00
// //
UIView *container = self.kb_keyboardContainer;
if ([container respondsToSelector:@selector(showPreviewForButton:)] &&
[container respondsToSelector:@selector(hidePreview)]) {
if (highlighted) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[container performSelector:@selector(showPreviewForButton:) withObject:self];
#pragma clang diagnostic pop
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[container performSelector:@selector(hidePreview)];
#pragma clang diagnostic pop
}
}
2025-10-28 20:11:40 +08:00
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
[self refreshStateAppearance];
}
- (void)refreshStateAppearance {
// Shift/CapsLock
2025-11-04 21:01:46 +08:00
KBSkinTheme *t = [KBSkinManager shared].current;
2025-10-28 20:11:40 +08:00
if (self.isSelected) {
2025-11-04 21:01:46 +08:00
self.backgroundColor = t.keyHighlightBackground ?: t.keyBackground;
2025-10-28 20:11:40 +08:00
} else {
2025-11-04 21:01:46 +08:00
self.backgroundColor = t.keyBackground;
2025-10-28 20:11:40 +08:00
}
2025-10-28 10:18:10 +08:00
}
2025-11-18 20:53:47 +08:00
- (void)applyThemeForCurrentKey {
//
// - identifier: letter_q
// - caseVariant: 0/1/2 => //
NSString *identifier = self.key.identifier;
2025-11-18 20:53:47 +08:00
NSInteger variant = (NSInteger)self.key.caseVariant;
UIImage *iconImg = [[KBSkinManager shared] iconImageForKeyIdentifier:identifier caseVariant:variant];
//
2025-11-18 20:53:47 +08:00
self.iconView.image = iconImg;
self.iconView.hidden = (iconImg == nil);
BOOL hasIcon = (iconImg != nil);
if (hasIcon) {
//
[self setTitle:@"" forState:UIControlStateNormal];
[self setTitle:@"" forState:UIControlStateHighlighted];
[self setTitle:@"" forState:UIControlStateSelected];
self.titleLabel.hidden = YES;
2025-11-20 18:23:56 +08:00
KBSkinTheme *t = [KBSkinManager shared].current;
t.keyBackground = [UIColor clearColor];
} else {
// 使 key.title hidden_keys
[self setTitle:self.key.title forState:UIControlStateNormal];
BOOL hideTextBySkin = [[KBSkinManager shared] shouldHideKeyTextForIdentifier:identifier];
self.titleLabel.hidden = hideTextBySkin;
}
2025-11-18 20:53:47 +08:00
}
2025-10-28 10:18:10 +08:00
@end
2025-11-20 21:11:27 +08:00
@implementation KBKeyButton (KBKeyboardContainer)
- (UIView *)kb_keyboardContainer {
UIView *v = self.superview;
while (v) {
// KBKeyboardView
if ([NSStringFromClass(v.class) isEqualToString:@"KBKeyboardView"]) {
return v;
}
v = v.superview;
}
return nil;
}
@end