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 ()
|
|
|
|
|
|
@property (nonatomic, strong) UIImageView *iconView;
|
|
|
|
|
|
@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-19 19:15:28 +08:00
|
|
|
|
iv.contentMode = UIViewContentModeScaleAspectFit;
|
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-10-28 20:11:40 +08:00
|
|
|
|
// 简单按压反馈:选中态不改变透明度,避免和高亮态冲突
|
|
|
|
|
|
if (self.isSelected) {
|
|
|
|
|
|
self.alpha = 1.0;
|
|
|
|
|
|
} else {
|
2025-10-28 21:27:01 +08:00
|
|
|
|
self.alpha = highlighted ? 0.2 : 1.0;
|
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 => 无变体/小写/大写
|
2025-11-19 16:13:30 +08:00
|
|
|
|
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-19 16:13:30 +08:00
|
|
|
|
|
|
|
|
|
|
// 设置整块按键背景图(若有)
|
2025-11-18 20:53:47 +08:00
|
|
|
|
self.iconView.image = iconImg;
|
|
|
|
|
|
self.iconView.hidden = (iconImg == nil);
|
2025-11-19 16:13:30 +08:00
|
|
|
|
|
|
|
|
|
|
BOOL hasIcon = (iconImg != nil);
|
|
|
|
|
|
|
|
|
|
|
|
if (hasIcon) {
|
|
|
|
|
|
// 有图标:仅显示图片,完全隐藏文字
|
|
|
|
|
|
[self setTitle:@"" forState:UIControlStateNormal];
|
|
|
|
|
|
[self setTitle:@"" forState:UIControlStateHighlighted];
|
|
|
|
|
|
[self setTitle:@"" forState:UIControlStateSelected];
|
|
|
|
|
|
self.titleLabel.hidden = YES;
|
|
|
|
|
|
} 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
|