优化键盘 预览宽度固定

This commit is contained in:
2025-11-21 13:48:22 +08:00
parent 31bb72c8f4
commit c371c7224e
2 changed files with 91 additions and 4 deletions

View File

@@ -39,7 +39,8 @@
[self addSubview:self.keyboardView];
[self.keyboardView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.top.equalTo(self.topBar.mas_bottom).offset(4);
// make.top.equalTo(self.topBar.mas_bottom).offset(4);
make.height.mas_equalTo(200);
make.bottom.equalTo(self.mas_bottom).offset(-4);
}];

View File

@@ -54,7 +54,7 @@
[self.row1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).offset(8);
make.left.right.equalTo(self);
make.height.mas_equalTo(44);
make.height.mas_equalTo(40);
}];
[self.row2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.row1.mas_bottom).offset(vSpacing);
@@ -276,6 +276,10 @@
}
- (void)buildRow:(UIView *)row withKeys:(NSArray<KBKey *> *)keys edgeSpacerMultiplier:(CGFloat)edgeSpacerMultiplier {
// 4 使
// 123/ABCAISend Space
BOOL isBottomControlRow = [self kb_isBottomControlRowWithKeys:keys];
CGFloat hInset = 6; //
CGFloat spacing = 0; //
UIView *previous = nil;
@@ -352,6 +356,7 @@
previous = btn;
}
// 使
[previous mas_makeConstraints:^(MASConstraintMaker *make) {
if (rightSpacer) {
@@ -361,6 +366,13 @@
}
}];
// 123/ABCAISend
// Space
if (isBottomControlRow) {
[self kb_applyBottomControlRowWidthInRow:row];
return;
}
//
KBKeyButton *firstChar = nil;
BOOL hasCharacterInRow = NO;
@@ -402,6 +414,7 @@
// 1:1
// - ShiftBackspace
// - 123/ABCModeChange#+=SymbolsToggleAICustom
// #+=
BOOL isBottomModeKey = (b.key.type == KBKeyTypeModeChange) ||
(b.key.type == KBKeyTypeSymbolsToggle) ||
(b.key.type == KBKeyTypeCustom);
@@ -409,7 +422,7 @@
b.key.type == KBKeyTypeBackspace ||
isBottomModeKey) {
[b mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(b.mas_height); // =
make.width.equalTo(b.mas_height).multipliedBy(1.2); // =
}];
continue;
}
@@ -445,6 +458,79 @@
}
}
#pragma mark - Row Helpers (Bottom Control Row)
// Space + Return ModeChange/SymbolsToggle
//
- (BOOL)kb_isBottomControlRowWithKeys:(NSArray<KBKey *> *)keys {
BOOL hasSpace = NO;
BOOL hasReturn = NO;
BOOL hasModeOrSymbols = NO;
for (KBKey *k in keys) {
if (k.type == KBKeyTypeCharacter) {
return NO;
}
if (k.type == KBKeyTypeSpace) {
hasSpace = YES;
} else if (k.type == KBKeyTypeReturn) {
hasReturn = YES;
} else if (k.type == KBKeyTypeModeChange || k.type == KBKeyTypeSymbolsToggle) {
hasModeOrSymbols = YES;
}
}
return hasSpace && hasReturn && hasModeOrSymbols;
}
//
// - 123/ABCAI =
// - Send = 2.4
// - Space
- (void)kb_applyBottomControlRowWidthInRow:(UIView *)row {
KBKeyButton *modeBtn = nil;
KBKeyButton *aiBtn = nil;
KBKeyButton *spaceBtn = nil;
KBKeyButton *retBtn = nil;
for (UIView *v in row.subviews) {
if (![v isKindOfClass:[KBKeyButton class]]) continue;
KBKeyButton *b = (KBKeyButton *)v;
switch (b.key.type) {
case KBKeyTypeModeChange:
case KBKeyTypeSymbolsToggle:
modeBtn = b;
break;
case KBKeyTypeCustom:
aiBtn = b;
break;
case KBKeyTypeSpace:
spaceBtn = b;
break;
case KBKeyTypeReturn:
retBtn = b;
break;
default:
break;
}
}
if (!modeBtn || !aiBtn || !spaceBtn || !retBtn) {
return;
}
// row1
[modeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(row.mas_height).multipliedBy(1.2);
}];
[aiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(row.mas_height).multipliedBy(1.2);
}];
[retBtn mas_makeConstraints:^(MASConstraintMaker *make) {
// Send 2.4
make.width.equalTo(modeBtn.mas_width).multipliedBy(2);
}];
// Space
}
#pragma mark - Actions
- (void)onKeyTapped:(KBKeyButton *)sender {
@@ -485,7 +571,7 @@
CGFloat centerX = CGRectGetMidX(btnFrameInSelf);
CGFloat centerY = CGRectGetMinY(btnFrameInSelf) - previewHeight * 0.6;
self.previewView.frame = CGRectMake(0, 0, previewWidth, previewHeight);
self.previewView.frame = CGRectMake(0, 0, 40, previewHeight);
self.previewView.center = CGPointMake(centerX, centerY);
self.previewView.alpha = 0.0;
self.previewView.hidden = NO;