Files
keyboard/CustomKeyboard/KeyboardViewController.m

142 lines
4.8 KiB
Mathematica
Raw Normal View History

2025-10-27 19:42:27 +08:00
//
// KeyboardViewController.m
// CustomKeyboard
//
// Created by Mac on 2025/10/27.
//
#import "KeyboardViewController.h"
2025-10-28 10:18:10 +08:00
#import "KBToolBar.h"
#import "KBKeyboardView.h"
#import "KBKey.h"
2025-10-27 19:42:27 +08:00
static CGFloat KEYBOARDHEIGHT = 256;
2025-10-28 10:18:10 +08:00
@interface KeyboardViewController () <KBToolBarDelegate, KBKeyboardViewDelegate>
@property (nonatomic, strong) UIButton *nextKeyboardButton; //
@property (nonatomic, strong) KBToolBar *topBar;
@property (nonatomic, strong) KBKeyboardView *keyboardView;
2025-10-27 19:42:27 +08:00
@end
@implementation KeyboardViewController
- (void)updateViewConstraints {
[super updateViewConstraints];
2025-10-28 10:18:10 +08:00
//
2025-10-27 19:42:27 +08:00
}
- (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 {
2025-10-28 10:18:10 +08:00
// 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);
}];
2025-10-27 19:42:27 +08:00
}
- (void)viewWillLayoutSubviews
{
self.nextKeyboardButton.hidden = !self.needsInputModeSwitchKey;
[super viewWillLayoutSubviews];
}
- (void)textWillChange:(id<UITextInput>)textInput {
2025-10-28 10:18:10 +08:00
//
2025-10-27 19:42:27 +08:00
}
- (void)textDidChange:(id<UITextInput>)textInput {
2025-10-28 10:18:10 +08:00
//
2025-10-27 19:42:27 +08:00
UIColor *textColor = nil;
if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
textColor = [UIColor whiteColor];
} else {
textColor = [UIColor blackColor];
}
[self.nextKeyboardButton setTitleColor:textColor forState:UIControlStateNormal];
}
2025-10-28 10:18:10 +08:00
#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;
}
}
2025-10-27 19:42:27 +08:00
@end