移动光标在开始UItextview

可能重复:
将光标移到UITextField的开头

你好,我有一个textview文本我想要在开始时移动光标位置我已经使用NSMakeRange,但我不知道为什么它不工作。 我写了NSMakeRange是不同的地方,希望它会运行至less一次,但没有工作。 这是代码。 thx提前

- (void)viewDidLoad { [super viewDidLoad]; apnatxtView.textColor=[UIColor lightGrayColor]; apnatxtView.text=@"Description goes here"; totalLenght=apnatxtView.text.length; apnatxtView.selectedRange=NSMakeRange(0,0); } - (BOOL)textViewShouldBeginEditing:(UITextView *)textView{ apnatxtView.selectedRange=NSMakeRange(0,0); return YES; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [apnatxtView resignFirstResponder]; } - (void)textViewDidChange:(UITextView *)textView{ if (apnatxtView.text.length == 0) { apnatxtView.textColor= [UIColor lightGrayColor]; apnatxtView.text=@"Description goes here"; apnatxtView.selectedRange=NSMakeRange(0, 0); } } - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if (apnatxtView.textColor == [UIColor lightGrayColor]) { apnatxtView.textColor=[UIColor blackColor]; // apnatxtView.text=@"Description goes here"; apnatxtView.text=nil; return YES; } } 

这在iOS 6.0模拟器上的testing中工作:

 - (void)textViewDidBeginEditing:(UITextView *)textView { dispatch_async(dispatch_get_main_queue(), ^{ textView.selectedRange = NSMakeRange(0, 0); }); } 

我猜它会发送textViewDidBeginEditing:消息根据触摸位置更新select。 dispatch_async可以解决这个问题。

K我find了解决scheme。 我设置游标出现之前的光标位置我剪切并粘贴在KeyBoardDidShow通知中的代码,它工作得很好。