3
This commit is contained in:
@@ -7,6 +7,10 @@
|
||||
#import "KBKey.h"
|
||||
#import "KBSkinManager.h"
|
||||
|
||||
@interface KBKeyButton ()
|
||||
@property (nonatomic, strong) UIImageView *iconView;
|
||||
@end
|
||||
|
||||
@implementation KBKeyButton
|
||||
|
||||
- (instancetype)initWithFrame:(CGRect)frame {
|
||||
@@ -29,6 +33,34 @@
|
||||
self.layer.shadowOffset = CGSizeMake(0, 1);
|
||||
self.layer.shadowRadius = 1.5;
|
||||
[self refreshStateAppearance];
|
||||
|
||||
// 懒创建图标视图,用于后续皮肤按键小图标展示
|
||||
if (!self.iconView) {
|
||||
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectZero];
|
||||
// 作为按键的整块皮肤背景,铺满整个按钮区域
|
||||
iv.contentMode = UIViewContentModeScaleAspectFill;
|
||||
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;
|
||||
// 每次切换按键模型时,根据皮肤刷新一次图标和文字显隐
|
||||
[self applyThemeForCurrentKey];
|
||||
}
|
||||
|
||||
- (void)setHighlighted:(BOOL)highlighted {
|
||||
@@ -56,4 +88,19 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)applyThemeForCurrentKey {
|
||||
// 依据皮肤决定是否显示文字
|
||||
NSString *identifier = self.key.identifier;
|
||||
BOOL hideText = [[KBSkinManager shared] shouldHideKeyTextForIdentifier:identifier];
|
||||
self.titleLabel.hidden = hideText;
|
||||
|
||||
// 根据皮肤映射加载图标(若有),支持大小写变体:
|
||||
// - identifier: 逻辑按键标识(如 letter_q)
|
||||
// - caseVariant: 0/1/2 => 无变体/小写/大写
|
||||
NSInteger variant = (NSInteger)self.key.caseVariant;
|
||||
UIImage *iconImg = [[KBSkinManager shared] iconImageForKeyIdentifier:identifier caseVariant:variant];
|
||||
self.iconView.image = iconImg;
|
||||
self.iconView.hidden = (iconImg == nil);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user