1
This commit is contained in:
@@ -6,12 +6,14 @@
|
||||
#import "KBKeyButton.h"
|
||||
#import "KBKey.h"
|
||||
#import "KBSkinManager.h"
|
||||
#import <QuartzCore/QuartzCore.h>
|
||||
|
||||
@interface KBKeyButton ()
|
||||
// 内部缓存:便于从按钮查找到所属的 KBKeyboardView
|
||||
@property (nonatomic, weak, readonly) UIView *kb_keyboardContainer;
|
||||
@property (nonatomic, strong) UIImageView *normalImageView; /// 没有皮肤的时候展示
|
||||
@property (nonatomic, strong) UIColor *baseBackgroundColor; /// 无按下状态下,由皮肤/主题决定的底色(由 normalImageView 展示)
|
||||
@property (nonatomic, strong) CAGradientLayer *bottomShadowLayer;
|
||||
|
||||
@end
|
||||
|
||||
@@ -24,8 +26,10 @@
|
||||
[NSLayoutConstraint activateConstraints:@[
|
||||
[self.normalImageView.topAnchor constraintEqualToAnchor:self.topAnchor],
|
||||
[self.normalImageView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
|
||||
[self.normalImageView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:2],
|
||||
[self.normalImageView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-2],
|
||||
// [self.normalImageView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:2],
|
||||
// [self.normalImageView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-2],
|
||||
[self.normalImageView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor constant:0],
|
||||
[self.normalImageView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor constant:-0],
|
||||
]];
|
||||
[self applyDefaultStyle];
|
||||
}
|
||||
@@ -48,6 +52,7 @@
|
||||
|
||||
// 初始状态下根据主题设置底色(给没有皮肤图的按键使用)
|
||||
[self refreshStateAppearance];
|
||||
[self kb_setupBottomShadowIfNeeded];
|
||||
|
||||
// 懒创建图标视图,用于后续皮肤按键小图标展示
|
||||
if (!self.iconView) {
|
||||
@@ -72,6 +77,20 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)layoutSubviews {
|
||||
[super layoutSubviews];
|
||||
if (!self.bottomShadowLayer) { return; }
|
||||
CGRect bounds = self.normalImageView.bounds;
|
||||
CGFloat shadowHeight = 2;
|
||||
if (CGRectGetHeight(bounds) <= 0 || CGRectGetWidth(bounds) <= 0) {
|
||||
return;
|
||||
}
|
||||
self.bottomShadowLayer.frame = CGRectMake(0,
|
||||
CGRectGetHeight(bounds) - shadowHeight,
|
||||
CGRectGetWidth(bounds),
|
||||
shadowHeight);
|
||||
}
|
||||
|
||||
- (void)setKey:(KBKey *)key {
|
||||
_key = key;
|
||||
}
|
||||
@@ -121,14 +140,25 @@
|
||||
[self refreshStateAppearance];
|
||||
}
|
||||
|
||||
- (void)setCustomBackgroundColor:(UIColor *)customBackgroundColor {
|
||||
_customBackgroundColor = customBackgroundColor;
|
||||
[self refreshStateAppearance];
|
||||
}
|
||||
|
||||
- (void)refreshStateAppearance {
|
||||
// 选中态用于 Shift/CapsLock 等特殊按键的高亮显示
|
||||
KBSkinTheme *t = [KBSkinManager shared].current;
|
||||
UIColor *base = nil;
|
||||
if (self.isSelected) {
|
||||
base = t.keyHighlightBackground ?: t.keyBackground;
|
||||
if (self.customBackgroundColor) {
|
||||
base = t.keyHighlightBackground ?: self.customBackgroundColor;
|
||||
}
|
||||
} else {
|
||||
base = t.keyBackground;
|
||||
base = self.customBackgroundColor ?: t.keyBackground;
|
||||
}
|
||||
if (self.customBackgroundColor && self.key.type == KBKeyTypeShift) {
|
||||
base = self.customBackgroundColor;
|
||||
}
|
||||
if (!base) {
|
||||
base = [UIColor whiteColor];
|
||||
@@ -138,6 +168,13 @@
|
||||
// 按键背景统一由 normalImageView 控制,按钮本身透明
|
||||
self.backgroundColor = [UIColor clearColor];
|
||||
|
||||
if (self.key.type == KBKeyTypeShift) {
|
||||
UIColor *textColor = self.isSelected ? [UIColor blackColor] : (t.keyTextColor ?: [UIColor blackColor]);
|
||||
[self setTitleColor:textColor forState:UIControlStateNormal];
|
||||
[self setTitleColor:textColor forState:UIControlStateHighlighted];
|
||||
[self setTitleColor:textColor forState:UIControlStateSelected];
|
||||
}
|
||||
|
||||
// 有皮肤图时仅展示 icon,不再显示普通背景色
|
||||
if (self.iconView.image != nil || self.normalImageView.hidden) {
|
||||
return;
|
||||
@@ -169,6 +206,7 @@
|
||||
|
||||
BOOL hasIcon = (iconImg != nil);
|
||||
self.normalImageView.hidden = hasIcon;
|
||||
self.bottomShadowLayer.hidden = hasIcon;
|
||||
if (hasIcon) {
|
||||
// 有图标:仅显示图片,完全隐藏文字
|
||||
[self setTitle:@"" forState:UIControlStateNormal];
|
||||
@@ -184,6 +222,19 @@
|
||||
}
|
||||
}
|
||||
|
||||
- (void)kb_setupBottomShadowIfNeeded {
|
||||
if (self.bottomShadowLayer) { return; }
|
||||
CAGradientLayer *layer = [CAGradientLayer layer];
|
||||
layer.startPoint = CGPointMake(0.5, 0.0);
|
||||
layer.endPoint = CGPointMake(0.5, 1.0);
|
||||
layer.colors = @[
|
||||
(id)[UIColor colorWithWhite:0 alpha:0.5].CGColor,
|
||||
(id)[UIColor colorWithWhite:0 alpha:0.7].CGColor
|
||||
];
|
||||
[self.normalImageView.layer addSublayer:layer];
|
||||
// self.bottomShadowLayer = layer;
|
||||
}
|
||||
|
||||
- (UIImageView *)normalImageView{
|
||||
if (!_normalImageView) {
|
||||
_normalImageView = [[UIImageView alloc] init];
|
||||
|
||||
Reference in New Issue
Block a user