在 UItextview 开始时移动光标 [英] Moving cursor at beginning UItextview
问题描述
可能的重复:
将光标移动到 UITextField 的开头
您好,我有一个带有文本的文本视图 我想在开始时移动光标位置 我使用了 NSMakeRange 但我不知道为什么它不起作用.我写过 NSMakeRange is different places ,希望它至少运行一次但没有用.这是代码.提前谢谢
Hello i have a textview with text I want to move cursor position at beginning I have use NSMakeRange but i don't know why its not working. I have written NSMakeRange is different places , hoping that it would run atleast once but didn't work. Here is the code. thx in advance
- (void)viewDidLoad
{
[super viewDidLoad];
apnatxtView.textColor=[UIColor lightGrayColor];
apnatxtView.text=@"Description goes here";
totalLenght=apnatxtView.text.length;
apnatxtView.selectedRange=NSMakeRange(0,0);
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
apnatxtView.selectedRange=NSMakeRange(0,0);
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[apnatxtView resignFirstResponder];
}
- (void)textViewDidChange:(UITextView *)textView{
if (apnatxtView.text.length == 0) {
apnatxtView.textColor= [UIColor lightGrayColor];
apnatxtView.text=@"Description goes here";
apnatxtView.selectedRange=NSMakeRange(0, 0);
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (apnatxtView.textColor == [UIColor lightGrayColor]) {
apnatxtView.textColor=[UIColor blackColor];
// apnatxtView.text=@"Description goes here";
apnatxtView.text=nil;
return YES;
}
}
推荐答案
这适用于我在 iOS 6.0 模拟器上的测试:
This works in my testing on the iOS 6.0 simulator:
- (void)textViewDidBeginEditing:(UITextView *)textView {
dispatch_async(dispatch_get_main_queue(), ^{
textView.selectedRange = NSMakeRange(0, 0);
});
}
我猜它会根据触摸位置更新选择之后它发送textViewDidBeginEditing:
消息.dispatch_async
可以解决这个问题.
I guess it updates the selection based on the touch location after it sends the textViewDidBeginEditing:
message. The dispatch_async
works around that.
这篇关于在 UItextview 开始时移动光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!