【问题标题】:Moving cursor at beginning UItextview [duplicate]在开始 UItextview 移动光标[重复]
【发布时间】:2012-11-20 07:30:30
【问题描述】:

可能重复:
Moving the cursor to the beginning of UITextField

您好,我有一个带有文本的文本视图,我想在开始时移动光标位置我使用了 NSMakeRange,但我不知道为什么它不起作用。我写过 NSMakeRange is different places ,希望它至少能运行一次但没有用。这是代码。提前谢谢

- (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;
    }

}

【问题讨论】:

  • @CodaFi UITextFieldUITextView 不一样,同样的代码可能不行。
  • 也许 TwUI 正在腐蚀我的大脑......

标签: ios uitextview


【解决方案1】:

这适用于我在 iOS 6.0 模拟器上的测试:

- (void)textViewDidBeginEditing:(UITextView *)textView {
    dispatch_async(dispatch_get_main_queue(), ^{
        textView.selectedRange = NSMakeRange(0, 0);
    });
}

我猜它会根据触摸位置更新选择它发送textViewDidBeginEditing: 消息。 dispatch_async 可以解决这个问题。

【讨论】:

  • 谢谢,它确实解决了我的问题。
  • 火锅咖啡。我在桌子上撞了一个小时,因为我没有在主队列中调度
【解决方案2】:

我找到了解决方案。我在光标出现之前设置光标位置我在 KeyBoardDidShow 通知中剪切并粘贴代码,它工作得很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多