修改功能
This commit is contained in:
@@ -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<UITextDocumentProxy> proxy = ivc.textDocumentProxy;
|
||||
[proxy deleteBackward];
|
||||
}
|
||||
- (void)onTapClear {
|
||||
NSLog(@"点击:清空");
|
||||
// 连续删除,直到当前输入位置之前无文本
|
||||
UIInputViewController *ivc = [self findInputViewController];
|
||||
id<UITextDocumentProxy> 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<UITextDocumentProxy> 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
|
||||
|
||||
Reference in New Issue
Block a user