diff --git a/CustomKeyboard/View/KBFunctionBarView.m b/CustomKeyboard/View/KBFunctionBarView.m index 6b1815b..4bc6a89 100644 --- a/CustomKeyboard/View/KBFunctionBarView.m +++ b/CustomKeyboard/View/KBFunctionBarView.m @@ -20,8 +20,8 @@ - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor clearColor]; - _leftTitles = @[@"帮回", @"会说", @"话术", @"更多"]; - _rightTitles = @[@"❤", @"收藏", @"宫格"]; + _leftTitles = @[@"ABC"]; + _rightTitles = @[@"Upgrade VIP"]; [self buildUI]; } return self; @@ -56,7 +56,7 @@ // 左侧4个等宽按钮 NSMutableArray *leftBtns = [NSMutableArray arrayWithCapacity:4]; UIView *prev = nil; - for (NSInteger i = 0; i < 4; i++) { + for (NSInteger i = 0; i < self.leftTitles.count; i++) { UIButton *btn = [self buildButtonWithTitle:(i < self.leftTitles.count ? self.leftTitles[i] : [NSString stringWithFormat:@"L%ld", (long)i])]; btn.tag = 100 + i; [btn addTarget:self action:@selector(onLeftTap:) forControlEvents:UIControlEventTouchUpInside]; @@ -81,7 +81,7 @@ // 右侧3个等宽按钮(靠右) NSMutableArray *rightBtns = [NSMutableArray arrayWithCapacity:3]; UIView *next = nil; - for (NSInteger i = 0; i < 3; i++) { + for (NSInteger i = 0; i < self.rightTitles.count; i++) { UIButton *btn = [self buildButtonWithTitle:(i < self.rightTitles.count ? self.rightTitles[i] : [NSString stringWithFormat:@"R%ld", (long)i])]; btn.tag = 200 + i; [self.rightContainer addSubview:btn]; diff --git a/CustomKeyboard/View/KBFunctionView.m b/CustomKeyboard/View/KBFunctionView.m index 28d58e1..3faa074 100644 --- a/CustomKeyboard/View/KBFunctionView.m +++ b/CustomKeyboard/View/KBFunctionView.m @@ -232,9 +232,39 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId"; } } } -- (void)onTapDelete { NSLog(@"点击:删除"); } -- (void)onTapClear { NSLog(@"点击:清空"); } -- (void)onTapSend { NSLog(@"点击:发送"); } +- (void)onTapDelete { + NSLog(@"点击:删除"); + // 单次删除一个字符(从宿主 App 的文本框光标前) + UIInputViewController *ivc = [self findInputViewController]; + id proxy = ivc.textDocumentProxy; + [proxy deleteBackward]; +} +- (void)onTapClear { + NSLog(@"点击:清空"); + // 连续删除,直到当前输入位置之前无文本 + UIInputViewController *ivc = [self findInputViewController]; + id proxy = ivc.textDocumentProxy; + // 为避免极端情况下的长时间阻塞,按块删除 + NSInteger guard = 0; + while (proxy.hasText && guard < 10000) { // 合理的上限保护 + NSString *before = proxy.documentContextBeforeInput ?: @""; + // 优先批量删除当前可见的 before 字符数 + NSInteger count = before.length > 0 ? before.length : 1; + for (NSInteger i = 0; i < count; i++) { + [proxy deleteBackward]; + } + guard += count; + } + // 同步重置功能区里的占位文案 + self.pasteView.placeholderLabel.text = @"点击粘贴TA的话"; +} +- (void)onTapSend { + NSLog(@"点击:发送"); + // 发送:插入换行。大多数聊天类 App 会把回车视为“发送” + UIInputViewController *ivc = [self findInputViewController]; + id proxy = ivc.textDocumentProxy; + [proxy insertText:@"\n"]; +} #pragma mark - Lazy @@ -354,4 +384,18 @@ static NSString * const kKBFunctionTagCellId = @"KBFunctionTagCellId"; - (UIButton *)clearButton { return self.clearButtonInternal; } - (UIButton *)sendButton { return self.sendButtonInternal; } +#pragma mark - Find Owner Controller + +// 在视图的响应链中查找宿主 UIInputViewController(KeyboardViewController) +- (UIInputViewController *)findInputViewController { + UIResponder *responder = self; + while (responder) { + if ([responder isKindOfClass:[UIInputViewController class]]) { + return (UIInputViewController *)responder; + } + responder = responder.nextResponder; + } + return nil; +} + @end diff --git a/keyBoard.xcworkspace/xcuserdata/mac.xcuserdatad/IDEFindNavigatorScopes.plist b/keyBoard.xcworkspace/xcuserdata/mac.xcuserdatad/IDEFindNavigatorScopes.plist new file mode 100644 index 0000000..5dd5da8 --- /dev/null +++ b/keyBoard.xcworkspace/xcuserdata/mac.xcuserdatad/IDEFindNavigatorScopes.plist @@ -0,0 +1,5 @@ + + + + +