2014-10-27 38 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]; 
}