142 lines
4.8 KiB
Objective-C
142 lines
4.8 KiB
Objective-C
//
|
|
// KeyboardViewController.m
|
|
// CustomKeyboard
|
|
//
|
|
// Created by Mac on 2025/10/27.
|
|
//
|
|
|
|
#import "KeyboardViewController.h"
|
|
#import "KBToolBar.h"
|
|
#import "KBKeyboardView.h"
|
|
#import "KBKey.h"
|
|
|
|
static CGFloat KEYBOARDHEIGHT = 256;
|
|
|
|
@interface KeyboardViewController () <KBToolBarDelegate, KBKeyboardViewDelegate>
|
|
@property (nonatomic, strong) UIButton *nextKeyboardButton; // 系统“下一个键盘”按钮(可选)
|
|
@property (nonatomic, strong) KBToolBar *topBar;
|
|
@property (nonatomic, strong) KBKeyboardView *keyboardView;
|
|
@end
|
|
|
|
@implementation KeyboardViewController
|
|
|
|
- (void)updateViewConstraints {
|
|
[super updateViewConstraints];
|
|
|
|
// 可在此添加自定义尺寸约束
|
|
}
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
//
|
|
// // Perform custom UI setup here
|
|
// self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];
|
|
//
|
|
// [self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
|
|
// [self.nextKeyboardButton sizeToFit];
|
|
// self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;
|
|
//
|
|
// [self.nextKeyboardButton addTarget:self action:@selector(handleInputModeListFromView:withEvent:) forControlEvents:UIControlEventAllTouchEvents];
|
|
//
|
|
// [self.view addSubview:self.nextKeyboardButton];
|
|
//
|
|
// [self.nextKeyboardButton.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
|
|
// [self.nextKeyboardButton.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
|
|
|
|
[self setupUI];
|
|
}
|
|
|
|
|
|
- (void)setupUI {
|
|
// self.view.translatesAutoresizingMaskIntoConstraints = NO;
|
|
// 固定键盘整体高度
|
|
[self.view.heightAnchor constraintEqualToConstant:KEYBOARDHEIGHT].active = YES;
|
|
|
|
// 顶部栏
|
|
self.topBar = [[KBToolBar alloc] init];
|
|
self.topBar.delegate = self;
|
|
[self.view addSubview:self.topBar];
|
|
[self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.equalTo(self.view);
|
|
make.top.equalTo(self.view.mas_top).offset(6);
|
|
make.height.mas_equalTo(40);
|
|
}];
|
|
|
|
// 键盘区域
|
|
self.keyboardView = [[KBKeyboardView alloc] init];
|
|
self.keyboardView.delegate = self;
|
|
[self.view addSubview:self.keyboardView];
|
|
[self.keyboardView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.equalTo(self.view);
|
|
make.top.equalTo(self.topBar.mas_bottom).offset(4);
|
|
make.bottom.equalTo(self.view.mas_bottom).offset(-4);
|
|
}];
|
|
}
|
|
|
|
|
|
- (void)viewWillLayoutSubviews
|
|
{
|
|
self.nextKeyboardButton.hidden = !self.needsInputModeSwitchKey;
|
|
[super viewWillLayoutSubviews];
|
|
}
|
|
|
|
- (void)textWillChange:(id<UITextInput>)textInput {
|
|
// 文档内容即将变化,可在此做准备
|
|
}
|
|
|
|
- (void)textDidChange:(id<UITextInput>)textInput {
|
|
// 文档内容刚发生变化,环境已更新
|
|
|
|
UIColor *textColor = nil;
|
|
if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
|
|
textColor = [UIColor whiteColor];
|
|
} else {
|
|
textColor = [UIColor blackColor];
|
|
}
|
|
[self.nextKeyboardButton setTitleColor:textColor forState:UIControlStateNormal];
|
|
}
|
|
|
|
#pragma mark - KBToolBarDelegate
|
|
|
|
- (void)toolBar:(KBToolBar *)toolBar didTapActionAtIndex:(NSInteger)index {
|
|
// 可根据业务透传或处理。示例:插入占位标记 [A1]..[A4]
|
|
NSString *placeholder = [NSString stringWithFormat:@"[A%ld]", (long)index+1];
|
|
[self.textDocumentProxy insertText:placeholder];
|
|
}
|
|
|
|
- (void)toolBarDidTapSettings:(KBToolBar *)toolBar {
|
|
// 通常可通过 openURL 或宿主处理打开设置页。
|
|
// 这里示例仅插入一个标记。
|
|
[self.textDocumentProxy insertText:@"[settings]"];
|
|
}
|
|
|
|
#pragma mark - KBKeyboardViewDelegate
|
|
|
|
- (void)keyboardView:(KBKeyboardView *)keyboard didTapKey:(KBKey *)key {
|
|
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 KBKeyTypeModeChange: {
|
|
// 切换 字母 <-> 数字 布局
|
|
keyboard.layoutStyle = (keyboard.layoutStyle == KBKeyboardLayoutStyleLetters) ? KBKeyboardLayoutStyleNumbers : KBKeyboardLayoutStyleLetters;
|
|
[keyboard reloadKeys];
|
|
} break;
|
|
case KBKeyTypeGlobe:
|
|
[self advanceToNextInputMode]; break;
|
|
case KBKeyTypeCustom:
|
|
// 自定义占位:切换语言或其它操作
|
|
[self.textDocumentProxy insertText:@"[lang]"]; break;
|
|
case KBKeyTypeShift:
|
|
// Shift 已在 KBKeyboardView 内部处理
|
|
break;
|
|
}
|
|
}
|
|
|
|
@end
|