在开始UItextview时移动光标

Aze*_*rue 2 uitextview ios

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

您好我有文本textview我想在开始时移动光标位置我已经使用NSMakeRange但我不知道为什么它不工作.我写过NSMakeRange是不同的地方,希望它至少运行一次但不起作用.这是代码.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;
    }

}
Run Code Online (Sandbox Code Playgroud)

rob*_*off 21

这适用于我在iOS 6.0模拟器上的测试:

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

我猜它会发送textViewDidBeginEditing:消息根据触摸位置更新选择.在dispatch_async周围的作品.