ios 如何在 UITextView 中设置光标的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8891072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:22:17  来源:igfitidea点击:

How to set the position of the cursor in UITextView

iosobjective-ciphoneuitextview

提问by GURU

I have an UITextView in my iPhone app which is editable.

我的 iPhone 应用程序中有一个可编辑的 UITextView。

New button is created inside the UITextView whenever user select a specific function.

每当用户选择特定功能时,就会在 UITextView 内创建新按钮。

As the button is always placed on the left side in the text view, I need to position the cursor on the right side of the button so that user can see what they are typing. I can't seem to find a documented (or undocumented) method to set location of the cursor.

由于按钮始终位于文本视图的左侧,我需要将光标定位在按钮的右侧,以便用户可以看到他们正在键入的内容。我似乎找不到记录(或未记录)的方法来设置光标的位置。

Does anybody have any ideas or has anybody else achieved anything similar?

有没有人有任何想法或其他人取得了类似的成果?

回答by Cashew

I know this is VERY late, but I thought I could help people stuck in this (like me).

我知道这已经很晚了,但我认为我可以帮助陷入困境的人(像我一样)。

The performSelector:withObject:afterDelayseems to work fine (the other answers just wouldn't work for some strange reason):

performSelector:withObject:afterDelay似乎做工精细(其他答案只是不会为一些奇怪的原因工作):

- (void)textViewDidBeginEditing:(UITextView *)inView 
{ 
[self performSelector:@selector(setCursorToBeginning:) withObject:inView afterDelay:0.01]; 
} 

- (void)setCursorToBeginning:(UITextView *)inView 
{ 
//you can change first parameter in NSMakeRange to wherever you want the cursor to move
inView.selectedRange = NSMakeRange(3, 0); 
} 

source: http://puppenspieler.tumblr.com/post/757819650/set-selectedrange-in-a-uitextview

来源:http: //puppenspieler.tumblr.com/post/757819650/set-selectedrange-in-a-uitextview

回答by Evan

textView.editable = YES;
textView.selectedRange = NSMakeRange(2, 0);

Set the selectedRange to a location with length of 0 and you probably also want the textView to be editable so also set that to YES.

将 selectedRange 设置为长度为 0 的位置,您可能还希望 textView 可编辑,因此也将其设置为 YES。

回答by acavanagh

Try something like this:

尝试这样的事情:

dispatch_async(dispatch_get_main_queue(), ^{
    inView.selectedRange = NSMakeRange(3, 0);
});

This will cause selectedRange to be executed on the main thread at the beginning of the next runloop.

这将导致 selectedRange 在下一个 runloop 开始时在主线程上执行。

回答by blwinters

I use the following to get to the beginning of the textView, using Swift 4:

我使用以下内容进入 textView 的开头,使用Swift 4

DispatchQueue.main.async {
    self.selectedTextRange = self.textRange(from: self.beginningOfDocument, to: self.beginningOfDocument)
}

回答by xhan

change selectedRangeof your textView. for example to place cursor at position 3:

selectedRange你的 textView 的变化。例如将光标放在位置 3:

[textView setSelectedRange:NSMakeRange(3, 0)];

[textView setSelectedRange:NSMakeRange(3, 0)];

In your case, added some spaces on the textView contents might helps. and observer textview 's textDidChanged eventto prevent these space will be deleted by user.

在您的情况下,在 textView 内容上添加一些空格可能会有所帮助。和观察者textview 's textDidChanged event防止这些空间将被用户删除。

回答by Rico Nguyen

If you want to make Texview placeholder is required or optional like me. See my hack

如果你想像我一样制作 Texview 占位符是必需的或可选的。看看我的黑客

 func textViewDidBeginEditing(textView: UITextView) {
        if textView.text.isEmpty {
          if self.text == "Required" || self.text == "Optional" {
            dispatch_async(dispatch_get_main_queue(), {
              textView.selectedRange = NSMakeRange(0, 0)
            });
          }
        }
      }

      func textViewDidEndEditing(textView: UITextView) {
        if CommonUtils.isEmptyString(textView.text) {
          if required {
            textView.text = "Required"
          } else {
            textView.text = "Optional"
          }
          textView.textColor = grayColor//
        }
      }

      func textViewDidChange(textView: UITextView) {
        if textView.text.isEmpty {
          if required {
            textView.text = "Required"
          } else {
            textView.text = "Optional"
          }
          textView.textColor = grayColor
          textView.selectedRange = NSMakeRange(0, 0)
        } else {
          if required {
            if textView.text.length > "Required".length {
              textView.text = self.text.replace("Required", withString: "")
            }
          } else {
            if textView.text.length > "Optional".length {
              textView.text = self.text.replace("Optional", withString: "")
            }
          }
          textView.textColor = blackColor//
        }
      }

Dont forget to register delegate: UITextViewDelegate

不要忘记注册委托: UITextViewDelegate