我有一个UITextView,它允许一些RichTextEditing。我希望每当用户点击特定位置的文本,光标应该移动到那里,编辑应该从那个位置开始。
问题:所发生的事情是,当我将光标拖到我选择的任何位置并写东西时,光标立即移动到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
请指引我。
发布于 2014-10-28 08:13:01
苹果的功能有一些问题。我在函数中添加了代码引用( 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];
}https://stackoverflow.com/questions/26596660
复制相似问题