如何从iOS中的自定义键盘将键值推入UITextView中 [英] How to push key value into UITextView from custom keyboard in iOS

查看:127
本文介绍了如何从iOS中的自定义键盘将键值推入UITextView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为UITextView构建虚拟键盘.一切正常,我可以显示键盘,卸下键盘以及显示击键.我正在使用"stringByReplacingCharactersInRange:withString"方法将新的击键放置到文本视图中.这是可行的,但是由于每次都会替换字符串,因此入口点将到达字符串的末尾.这一切都是有道理的,但不是很有用.我可以通过在代码中进行检查来解决此问题,但是可能会有更好的结果……

I am building a virtual keyboard for a UITextView. All works well, I can display the keyboard and remove the keyboard as well as display the keystrokes. I am using the "stringByReplacingCharactersInRange: withString" method to place the new keystroke into the text view. This works but since it is replacing the string each time, the entry point goes to the end of the string. This all makes sense, but is not very usable. I can fix this by doing checks in my code but there might be something better...

所以我的问题是这个;有没有一种方法可以直接将新键直接推入UITextView,而无需手动修改UITextView的text属性的字符串?该手册指出,我必须自己管理目标/操作,但是我想知道是否错过了要调用的操作.看起来Apple Default键盘必须具有一种巧妙的方式将击键击入对象,而无需进行大量检查才能使键入变得流畅.

So my question is this; Is there a way to push the new key to the UITextView directly without modifying the UITextView's text property's string manually? The manual states that I will have to manage the target/action myself, but I was wondering if I've missed an action to call. It seems that the Apple Default keyboard must have a slick way of punching the keystroke into the object without the obvious number of checks that I will have to do to make the typing smooth.

任何帮助将不胜感激. 谢谢, 罗布

Any help will be greatly appreciated. Thanks, Rob

推荐答案

好吧,我没有收到任何回复.因此,我想出了一种方法来做到这一点,它既合理又简单.这是跟踪选定范围的问题.因此,当我希望退格时,我会使用这种技术.

Well, I didn't get any responses. So I figured out a way to do it so it is reasonable and simple. It's a matter of tracking the selected range. So when I wish to Backspace I use this technique.

NSRange currentRange = self.myTextView.selectedRange;
if (currentRange.length == 0) {
    currentRange.location--;
    currentRange.length++;
}
self.myTextView.text = [self.myTextView.text stringByReplacingCharactersInRange:currentRange withString:[NSString string]];
currentRange.length = 0;
self.myTextView.selectedRange = currentRange;

如果我需要键入一个字符,则可以使用.

If I need to type a character this works.

NSRange currentRange = self.myTextView.selectedRange;
self.myTextView.text = [self.myTextView.text stringByReplacingCharactersInRange:currentRange withString:someCharacter];
currentRange.location++;
currentRange.length = 0;
self.myTextView.selectedRange = currentRange;

如果有人有更好的方法,我想听听.

If anyone has a better way, I'd like to hear it.

谢谢,
罗布

这篇关于如何从iOS中的自定义键盘将键值推入UITextView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆