2012-02-19 130 views
1

当您选择(触摸)UITextView来模拟UITextField placeholder事物的种类时,我尝试移动光标。在UITextView中移动光标

我希望光标位于第一行的开头。我的问题是,[someTextField setSelectedRange]不能可靠地工作。当我在textView:shouldChangeTextInRange:replacementText:中调用它时,它应该可以正常工作。但是这种方法只在用户开始输入时才被调用。我使用textViewDidBeginEditing:移动光标时UITextView成为第一个响应者:

- (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 2012-04-13 15:46:22

回答

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); 

希望这会有所帮助。