2014-10-27 21 views
1

我有一個UITextView它允許一些RichTextEditing。我希望每當用戶點擊文本的某個位置時,光標應該移動到那裏,並且應該從該位置開始編輯。開始編輯用戶抽頭位置上的UITextView

問題: 會發生什麼事,當我拖動光標以我所選擇的任何位置,寫的東西馬上光標移動到的UITextView的文本的結束和開始寫作。我想要編輯從用戶點擊位置或光標位置開始。

以下是我正在使用的功能。

-(void) textViewDidBeginEditing{ 
    //Can set the cursor or selected Range of textView here or tap gesture recognizer required? 
    return true; 
} 
-(void) textViewDidEndEditing{ 
    return true; 
} 
-(void) textViewDidChangeTextInRange:(UITextView *) textView range:(NSMakeRange) range replacementText: (NSString *) text{ 
    //Sets attributed text of my TextView based on my input AccessoryView 
    if(textView.text.lenght > range.location){ 
     //This is my point of concern where I want to start the editing 
    } 
    return false; 
} 

對不起,我的代碼錯誤。我現在無法訪問我的objectiveC代碼。這是寫給你一個想法,我在做什麼。 我發現這個,但不能得到它的工作。我正在使用它正在使用的字段的UITextView。 how to move cursor in UITextField after setting its value

請親引導我。

回答

0

Apple函數存在某種問題。 我這裏補充 how to move cursor in UITextField after setting its value 代碼引用我的功能

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ 
    //code from stackoverflow question 
} 

,但沒有奏效。 然後,我在另一個函數中添加了相同的代碼,並將其命名爲shouldChangeTextInRange並且它起作用!!!

工作代碼:

- (void)setCursorToCorrectPosition 
{ 
    //code from stackoverflow question 
} 
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ 
    [self performSelector:@selector(setCursorToCorrectPosition) withObject:nil afterDelay:0.01]; 
}