2012-02-19 15 views
1

UITextViewが選択(タッチ)されたときにカーソルを移動して、UITextField placeholderの種類をシミュレートしようとしました。UITextViewでカーソルを移動

私はカーソルを最初の行の先頭にしたいと思います。私の問題は、[someTextField setSelectedRange]が確実に動作していないことです。私がtextView:shouldChangeTextInRange:replacementText:でそれを呼び出すと、それはうまく動作します。しかし、このメソッドは、ユーザーが入力を開始したときにのみ呼び出されます。 UITextViewがファーストレスポンダになったとき、私は、カーソルを移動するtextViewDidBeginEditing:を使用しています:

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    if (textView == self.descriptionText) { 
     CustomTextView* customTV = (CustomTextView *)textView; 
     if ([customTV.text isEqualToString:customTV.placeholder]) { 
      // text in text view is still the placeholder -> move cursor to the beginning 
      customTV.text = customTV.placeholder; 
      customTV.textColor = [UIColor lightGrayColor]; 
      customTV.selectedRange = NSMakeRange(0, 0); 
     } 
    } 
} 

なぜcustomTV.selectedRange = NSMakeRange(0, 0);textViewDidBeginEditing:で正しく動作していない任意のアイデア?

ありがとうございました!

+0

解決方法を見つけましたか? – Legolas

答えて

2

実際には、これを達成するための非常に簡単な方法があります。

// Count the characters on screen 
    NSMutableString *numOfChar = [self.myTextField.text mutableCopy]; 

    // Skip that many characters to the left 
    self.myTextField.selectedRange = NSMakeRange(self.myTextField.selectedRange.location-[numOfChar length], 0); 

これが役に立ちます。

関連する問題