This commit is contained in:
2026-01-08 16:54:38 +08:00
parent bdf2a9af80
commit 6f80f969a4
9 changed files with 1121 additions and 31 deletions

View File

@@ -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];