2025-10-27 19:42:27 +08:00
|
|
|
|
//
|
|
|
|
|
|
// KeyboardViewController.m
|
|
|
|
|
|
// CustomKeyboard
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by Mac on 2025/10/27.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#import "KeyboardViewController.h"
|
2025-10-28 15:10:38 +08:00
|
|
|
|
#import "KBKeyBoardMainView.h"
|
|
|
|
|
|
|
2025-10-28 10:18:10 +08:00
|
|
|
|
#import "KBKey.h"
|
2025-10-28 15:10:38 +08:00
|
|
|
|
#import "KBFunctionView.h"
|
|
|
|
|
|
#import "Masonry.h"
|
2025-10-27 19:42:27 +08:00
|
|
|
|
|
2025-10-28 15:34:19 +08:00
|
|
|
|
static CGFloat KEYBOARDHEIGHT = 256 + 20;
|
2025-10-27 19:42:27 +08:00
|
|
|
|
|
2025-10-28 15:18:12 +08:00
|
|
|
|
@interface KeyboardViewController () <KBKeyBoardMainViewDelegate, KBFunctionViewDelegate>
|
2025-10-28 10:18:10 +08:00
|
|
|
|
@property (nonatomic, strong) UIButton *nextKeyboardButton; // 系统“下一个键盘”按钮(可选)
|
2025-10-28 15:10:38 +08:00
|
|
|
|
@property (nonatomic, strong) KBKeyBoardMainView *keyBoardMainView; // 功能面板视图(点击工具栏第0个时显示)
|
|
|
|
|
|
@property (nonatomic, strong) KBFunctionView *functionView; // 功能面板视图(点击工具栏第0个时显示)
|
2025-10-27 19:42:27 +08:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation KeyboardViewController
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
|
|
|
|
|
[super viewDidLoad];
|
|
|
|
|
|
[self setupUI];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)setupUI {
|
2025-10-28 10:18:10 +08:00
|
|
|
|
// 固定键盘整体高度
|
|
|
|
|
|
[self.view.heightAnchor constraintEqualToConstant:KEYBOARDHEIGHT].active = YES;
|
2025-10-28 15:10:38 +08:00
|
|
|
|
// 预置功能面板(默认隐藏),与键盘区域共享相同布局
|
|
|
|
|
|
self.functionView.hidden = YES;
|
|
|
|
|
|
[self.view addSubview:self.functionView];
|
|
|
|
|
|
[self.functionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
2025-10-28 10:18:10 +08:00
|
|
|
|
make.left.right.equalTo(self.view);
|
2025-10-28 15:10:38 +08:00
|
|
|
|
make.top.equalTo(self.view).offset(4);
|
|
|
|
|
|
make.bottom.equalTo(self.view.mas_bottom).offset(-4);
|
2025-10-28 10:18:10 +08:00
|
|
|
|
}];
|
2025-10-28 15:10:38 +08:00
|
|
|
|
|
|
|
|
|
|
[self.view addSubview:self.keyBoardMainView];
|
|
|
|
|
|
[self.keyBoardMainView mas_makeConstraints:^(MASConstraintMaker *make) {
|
2025-10-28 10:18:10 +08:00
|
|
|
|
make.left.right.equalTo(self.view);
|
2025-10-28 15:10:38 +08:00
|
|
|
|
make.top.equalTo(self.view).offset(4);
|
2025-10-28 10:18:10 +08:00
|
|
|
|
make.bottom.equalTo(self.view.mas_bottom).offset(-4);
|
|
|
|
|
|
}];
|
2025-10-27 19:42:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-28 10:18:10 +08:00
|
|
|
|
|
2025-10-28 15:10:38 +08:00
|
|
|
|
#pragma mark - Private
|
2025-10-28 14:30:03 +08:00
|
|
|
|
|
2025-10-28 15:10:38 +08:00
|
|
|
|
/// 切换显示功能面板/键盘主视图
|
|
|
|
|
|
- (void)showFunctionPanel:(BOOL)show {
|
|
|
|
|
|
// 简单显隐切换,复用相同的布局区域
|
|
|
|
|
|
self.functionView.hidden = !show;
|
|
|
|
|
|
self.keyBoardMainView.hidden = show;
|
2025-10-28 15:18:12 +08:00
|
|
|
|
|
2025-10-28 15:10:38 +08:00
|
|
|
|
// 可选:把当前显示的视图置顶,避免层级遮挡
|
|
|
|
|
|
if (show) {
|
|
|
|
|
|
[self.view bringSubviewToFront:self.functionView];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
[self.view bringSubviewToFront:self.keyBoardMainView];
|
|
|
|
|
|
}
|
2025-10-28 10:18:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-28 15:10:38 +08:00
|
|
|
|
|
|
|
|
|
|
// MARK: - KBKeyBoardMainViewDelegate
|
|
|
|
|
|
- (void)keyBoardMainView:(KBKeyBoardMainView *)keyBoardMainView didTapKey:(KBKey *)key {
|
2025-10-28 10:18:10 +08:00
|
|
|
|
switch (key.type) {
|
|
|
|
|
|
case KBKeyTypeCharacter:
|
|
|
|
|
|
[self.textDocumentProxy insertText:key.output ?: key.title ?: @""]; break;
|
|
|
|
|
|
case KBKeyTypeBackspace:
|
|
|
|
|
|
[self.textDocumentProxy deleteBackward]; break;
|
|
|
|
|
|
case KBKeyTypeSpace:
|
|
|
|
|
|
[self.textDocumentProxy insertText:@" "]; break;
|
|
|
|
|
|
case KBKeyTypeReturn:
|
|
|
|
|
|
[self.textDocumentProxy insertText:@"\n"]; break;
|
|
|
|
|
|
case KBKeyTypeGlobe:
|
|
|
|
|
|
[self advanceToNextInputMode]; break;
|
|
|
|
|
|
case KBKeyTypeCustom:
|
|
|
|
|
|
[self.textDocumentProxy insertText:@"[lang]"]; break;
|
2025-10-28 15:10:38 +08:00
|
|
|
|
case KBKeyTypeModeChange:
|
2025-10-28 10:18:10 +08:00
|
|
|
|
case KBKeyTypeShift:
|
2025-10-28 15:10:38 +08:00
|
|
|
|
// 这些已在 KBKeyBoardMainView/KBKeyboardView 内部处理
|
2025-10-28 10:18:10 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-28 15:10:38 +08:00
|
|
|
|
- (void)keyBoardMainView:(KBKeyBoardMainView *)keyBoardMainView didTapToolActionAtIndex:(NSInteger)index {
|
|
|
|
|
|
if (index == 0) {
|
|
|
|
|
|
[self showFunctionPanel:YES];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
[self showFunctionPanel:NO];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-28 15:18:12 +08:00
|
|
|
|
// MARK: - KBFunctionViewDelegate
|
|
|
|
|
|
- (void)functionView:(KBFunctionView *)functionView didTapToolActionAtIndex:(NSInteger)index {
|
|
|
|
|
|
// 需求:当 index == 0 时,切回键盘主视图
|
|
|
|
|
|
if (index == 0) {
|
|
|
|
|
|
[self showFunctionPanel:NO];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-28 15:10:38 +08:00
|
|
|
|
#pragma mark - lazy
|
|
|
|
|
|
- (KBKeyBoardMainView *)keyBoardMainView{
|
|
|
|
|
|
if (!_keyBoardMainView) {
|
|
|
|
|
|
_keyBoardMainView = [[KBKeyBoardMainView alloc] init];
|
2025-10-28 15:18:12 +08:00
|
|
|
|
_keyBoardMainView.delegate = self;
|
2025-10-28 15:10:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
return _keyBoardMainView;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (KBFunctionView *)functionView{
|
|
|
|
|
|
if (!_functionView) {
|
|
|
|
|
|
_functionView = [[KBFunctionView alloc] init];
|
2025-10-28 15:18:12 +08:00
|
|
|
|
_functionView.delegate = self; // 监听功能面板顶部Bar点击
|
2025-10-28 15:10:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
return _functionView;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-28 15:18:12 +08:00
|
|
|
|
|
|
|
|
|
|
@end
|