优化联想

This commit is contained in:
2026-03-06 10:45:13 +08:00
parent bb74a330db
commit 33a04186fb
2 changed files with 121 additions and 36 deletions

View File

@@ -683,6 +683,11 @@ static inline NSUInteger kb_composedCharacterCount(NSString *string) {
self.kb_variantBaseOutput = (button.key.output.length > 0 ? button.key.output : button.key.title) ?: @"";
self.kb_variantBaseDeleted = NO;
self.kb_variantBaseDeleteCount = kb_composedCharacterCount(self.kb_variantBaseOutput);
if (@available(iOS 10.0, *)) {
UIImpactFeedbackGenerator *gen = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
[gen prepare];
[gen impactOccurred];
}
if (!self.kb_variantPopupView) {
self.kb_variantPopupView = [[KBKeyVariantPopupView alloc] initWithFrame:CGRectZero];

View File

@@ -8,15 +8,37 @@
#import "KBSkinManager.h"
@interface KBSuggestionBarView ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIStackView *stackView;
@property (nonatomic, strong) UIView *topLineView;
@property (nonatomic, strong) UIView *bottomLineView;
@property (nonatomic, copy) NSArray<NSString *> *items;
@property (nonatomic, strong) UIColor *pillColor;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, strong) UIColor *separatorColor;
@property (nonatomic, strong) UIColor *highlightColor;
@end
@implementation KBSuggestionBarView
- (NSInteger)kb_buttonTag {
return 10001;
}
- (NSInteger)kb_separatorTag {
return 10002;
}
- (UIImage *)kb_imageWithColor:(UIColor *)color {
if (!color) { color = [UIColor clearColor]; }
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, color.CGColor);
CGContextFillRect(ctx, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor clearColor];
@@ -26,15 +48,23 @@
}
- (void)setupUI {
[self addSubview:self.scrollView];
[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
[self addSubview:self.stackView];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.top.equalTo(self);
make.bottom.equalTo(self);
}];
[self.scrollView addSubview:self.stackView];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.scrollView).insets(UIEdgeInsetsMake(0, 8, 0, 8));
make.height.equalTo(self.scrollView);
[self addSubview:self.topLineView];
[self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalTo(self);
make.height.mas_equalTo(0.5);
}];
[self addSubview:self.bottomLineView];
[self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self);
make.height.mas_equalTo(0.5);
}];
[self applyTheme:[KBSkinManager shared].current];
@@ -48,35 +78,75 @@
[view removeFromSuperview];
}
for (NSString *item in self.items) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.layer.cornerRadius = 12.0;
NSUInteger count = self.items.count;
for (NSUInteger idx = 0; idx < count; idx++) {
NSString *item = self.items[idx];
UIView *container = [UIView new];
container.backgroundColor = UIColor.clearColor;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = [self kb_buttonTag];
btn.backgroundColor = UIColor.clearColor;
btn.layer.cornerRadius = 8.0;
btn.layer.masksToBounds = YES;
btn.backgroundColor = self.pillColor ?: [UIColor colorWithWhite:1 alpha:0.9];
btn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
btn.adjustsImageWhenHighlighted = NO;
btn.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
btn.titleLabel.adjustsFontSizeToFitWidth = YES;
btn.titleLabel.minimumScaleFactor = 0.85;
btn.contentEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 12);
[btn setTitle:item forState:UIControlStateNormal];
[btn setTitleColor:self.textColor ?: [UIColor blackColor] forState:UIControlStateNormal];
btn.contentEdgeInsets = UIEdgeInsetsMake(4, 10, 4, 10);
[btn setTitleColor:self.textColor ?: UIColor.blackColor forState:UIControlStateNormal];
if (self.highlightColor) {
[btn setBackgroundImage:[self kb_imageWithColor:self.highlightColor] forState:UIControlStateHighlighted];
}
[btn addTarget:self action:@selector(onTapSuggestion:) forControlEvents:UIControlEventTouchUpInside];
[self.stackView addArrangedSubview:btn];
[container addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(container);
}];
if (idx + 1 < count) {
UIView *sep = [UIView new];
sep.tag = [self kb_separatorTag];
sep.userInteractionEnabled = NO;
sep.backgroundColor = self.separatorColor ?: [UIColor colorWithWhite:0 alpha:0.12];
[container addSubview:sep];
[sep mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(container.mas_right);
make.centerY.equalTo(container.mas_centerY);
make.width.mas_equalTo(0.5);
make.height.equalTo(container.mas_height).multipliedBy(0.55);
}];
}
[self.stackView addArrangedSubview:container];
}
self.hidden = (self.items.count == 0);
}
- (void)applyTheme:(KBSkinTheme *)theme {
UIColor *bg = theme.keyBackground ?: [UIColor whiteColor];
UIColor *text = theme.keyTextColor ?: [UIColor blackColor];
UIColor *barBg = [UIColor colorWithHex:0xD1D3DB];
UIColor *barBg = theme.keyboardBackground ?: [UIColor colorWithWhite:0.95 alpha:1.0];
UIColor *text = theme.keyTextColor ?: UIColor.blackColor;
UIColor *highlight = theme.keyHighlightBackground ?: [UIColor colorWithWhite:0.85 alpha:1.0];
self.backgroundColor = barBg;
self.pillColor = bg;
self.textColor = text;
self.separatorColor = [text colorWithAlphaComponent:0.12];
self.highlightColor = [highlight colorWithAlphaComponent:0.25];
self.topLineView.backgroundColor = [text colorWithAlphaComponent:0.08];
self.bottomLineView.backgroundColor = [text colorWithAlphaComponent:0.06];
for (UIView *view in self.stackView.arrangedSubviews) {
if (![view isKindOfClass:[UIButton class]]) { continue; }
UIButton *btn = (UIButton *)view;
btn.backgroundColor = self.pillColor;
[btn setTitleColor:self.textColor forState:UIControlStateNormal];
UIButton *btn = (UIButton *)[view viewWithTag:[self kb_buttonTag]];
if ([btn isKindOfClass:UIButton.class]) {
[btn setTitleColor:self.textColor ?: UIColor.blackColor forState:UIControlStateNormal];
[btn setBackgroundImage:[self kb_imageWithColor:self.highlightColor] forState:UIControlStateHighlighted];
}
UIView *sep = [view viewWithTag:[self kb_separatorTag]];
if (sep) {
sep.backgroundColor = self.separatorColor ?: [UIColor colorWithWhite:0 alpha:0.12];
}
}
}
@@ -92,23 +162,33 @@
#pragma mark - Lazy
- (UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] init];
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.alwaysBounceHorizontal = YES;
}
return _scrollView;
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.spacing = 8.0;
_stackView.alignment = UIStackViewAlignmentFill;
_stackView.distribution = UIStackViewDistributionFillEqually;
_stackView.spacing = 0.0;
_stackView.layoutMarginsRelativeArrangement = YES;
_stackView.layoutMargins = UIEdgeInsetsMake(0, 8, 0, 8);
}
return _stackView;
}
- (UIView *)topLineView {
if (!_topLineView) {
_topLineView = [UIView new];
_topLineView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.06];
}
return _topLineView;
}
- (UIView *)bottomLineView {
if (!_bottomLineView) {
_bottomLineView = [UIView new];
_bottomLineView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.04];
}
return _bottomLineView;
}
@end