简体   繁体   English

在插入位置将文本插入UITextField

[英]Insert text into UITextField at caret Position

Is there any simple way to insert text at the caret position in a UITextField? 有没有简单的方法在UITextField中的插入位置插入文本? I have a custom keypad with some special characters and I'm having difficulty making this work. 我有一个带有一些特殊字符的自定义键盘,我很难完成这项工作。 I do not allow arbitrary input, the keypad is limited to about 20 special characters, and all others are rejected. 不允许随意输入,键盘被限制在大约20特殊字符,以及所有其他被拒绝。

The method of using the global clipboard to insert is a bad one since with VoiceOver turned on it says "Paste" instead of the character inserted every time you use it. 使用全局剪贴板插入的方法很糟糕,因为启用VoiceOver时会显示“粘贴”而不是每次使用时插入的字符。 Which is not good enough. 哪个不够好。

Finding any alternate solutions seems quite difficult since all just reference the paste solution. 找到任何替代解决方案似乎都很困难,因为所有人都只是参考了粘贴解

The solution was simpler than I thought: 解决方案比我想象的简单:

[self insertText:string];
UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, string);

This will work properly with selection, caret position et. 这将适用于选择,插入位置等。 al. 人。

It will however not use the proper tone when announcing the entered string. 但是,在宣布输入的字符串时,它不会使用正确的音调。 VoiceOver uses a deeper voice when announcing inserted text, I put up a separate question on how to control the tone of VoiceOver . VoiceOver在宣布插入的文本时使用了更深的声音,我提出了一个关于如何控制VoiceOver音调的单独问题

A UITextField implements the UITextInput protocol, and thus has a 'selectedTextRange' property, which returns either the selection or the cursor location. UITextField实现UITextInput协议,因此具有'selectedTextRange'属性,该属性返回选择或光标位置。 Read up on it as you can get lots of interesting information from the methods required in that protocol. 阅读它,因为您可以从该协议所需的方法中获得许多有趣的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM