如何在UITextView中设置光标的位置 [英] How to set the position of the cursor in UITextView

查看:283
本文介绍了如何在UITextView中设置光标的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iPhone应用程序中有一个可编辑的UITextView。

I have an UITextView in my iPhone app which is editable.

只要用户选择特定功能,就会在UITextView内创建新按钮。

New button is created inside the UITextView whenever user select a specific function.

由于按钮始终位于文本视图的左侧,我需要将光标放在按钮的右侧,以便用户可以看到它们的内容正在打字。
我似乎无法找到一个记录(或未记录)的方法来设置光标的位置。

As the button is always placed on the left side in the text view, I need to position the cursor on the right side of the button so that user can see what they are typing. I can't seem to find a documented (or undocumented) method to set location of the cursor.

是否有人有任何想法或有任何其他人实现类似的东西?

Does anybody have any ideas or has anybody else achieved anything similar?

推荐答案

我知道这已经很晚了,但我想我可以帮助那些陷入困境的人(像我一样)。

I know this is VERY late, but I thought I could help people stuck in this (like me).

performSelector:withObject:afterDelay 似乎工作正常(其他答案对某些奇怪的人不起作用)原因):

The performSelector:withObject:afterDelay seems to work fine (the other answers just wouldn't work for some strange reason):

- (void)textViewDidBeginEditing:(UITextView *)inView 
{ 
[self performSelector:@selector(setCursorToBeginning:) withObject:inView afterDelay:0.01]; 
} 

- (void)setCursorToBeginning:(UITextView *)inView 
{ 
//you can change first parameter in NSMakeRange to wherever you want the cursor to move
inView.selectedRange = NSMakeRange(3, 0); 
} 

来源:http://puppenspieler.tumblr.com/post/757819650/set-selectedrange-in-a-uitextview

这篇关于如何在UITextView中设置光标的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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