2

Possible Duplicate:
Moving the cursor to the beginning of UITextField

Hello i have a textview with text I want to move cursor position at beginning I have use NSMakeRange but i don't know why its not working. I have written NSMakeRange is different places , hoping that it would run atleast once but didn't work. Here is the code. thx in advance

- (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;
    }

}
Community
  • 1
  • 1
Azerue
  • 258
  • 6
  • 16

2 Answers2

21

This works in my testing on the iOS 6.0 simulator:

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

I guess it updates the selection based on the touch location after it sends the textViewDidBeginEditing: message. The dispatch_async works around that.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

K i found the solution. Im setting the cursor position before cursor appears I cut and paste the code in KeyBoardDidShow notification and it worked pretty fine.

Azerue
  • 258
  • 6
  • 16