将光标移动到 UITextField 的开头

发布于 2024-10-01 23:10:11 字数 75 浏览 2 评论 0原文

有没有办法让光标位于 UITextField 的开头?

当我显示带有内容的控件时,光标放置在文本的末尾。我想把它移到开头。

Is there a way to make the cursor be at the start of a UITextField?

When I display the control with content, the cursor is placed at the end of the text. I'd like to move it to the beginning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春夜浅 2024-10-08 23:10:11

UITextField 符合 UITextInput 协议,它提供了让您控制所选范围的方法。这在我的测试中有效:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.selectedTextRange = [textField
        textRangeFromPosition:textField.beginningOfDocument
        toPosition:textField.beginningOfDocument];
}

UITextField conforms to the UITextInput protocol, which provides methods that let you control the selected range. This works in my testing:

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.selectedTextRange = [textField
        textRangeFromPosition:textField.beginningOfDocument
        toPosition:textField.beginningOfDocument];
}
半透明的墙 2024-10-08 23:10:11

你正在与这个系统作斗争。 UITextField 没有任何公共属性来设置光标位置(实际上与当前选择的开头相关)。如果您可以使用 UITextView 代替,则以下委托方法将强制光标位于文本的开头。请注意,用户不会期望这种行为,您应该仔细检查您想要这样做的动机。

- (void)textViewDidBeginEditing:(UITextView *)textView {
    shouldMoveCursor = YES;
}

- (void)textViewDidChangeSelection:(UITextView *)textView {
    if(shouldMoveCursor)
    {
        NSRange beginningRange = NSMakeRange(0, 0);
        NSRange currentRange = [textView selectedRange];
        if(!NSEqualRanges(beginningRange, currentRange))
            [textView setSelectedRange:beginningRange];
        shouldMoveCursor = NO;
    }
}

其中 shouldMoveCursor 是您在控制器中维护的 BOOL 变量。

You're fighting the system on this one. UITextField does not have any public properties to set the cursor position (which actually correlates to the beginning of the current selection). If you can use a UITextView instead, the following delegate methods will force the cursor to the beginning of the text. Just be aware that users won't expect this behavior and you should double-check your motives for wanting to do it.

- (void)textViewDidBeginEditing:(UITextView *)textView {
    shouldMoveCursor = YES;
}

- (void)textViewDidChangeSelection:(UITextView *)textView {
    if(shouldMoveCursor)
    {
        NSRange beginningRange = NSMakeRange(0, 0);
        NSRange currentRange = [textView selectedRange];
        if(!NSEqualRanges(beginningRange, currentRange))
            [textView setSelectedRange:beginningRange];
        shouldMoveCursor = NO;
    }
}

Where shouldMoveCursor is a BOOL variable you maintain in your controller.

动次打次papapa 2024-10-08 23:10:11

为我工作

// Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];
// Calculate the new position, - for left and + for right
UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:3];
// Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:newPosition];
// Set new range
[textField setSelectedTextRange:newRange];

works for me

// Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];
// Calculate the new position, - for left and + for right
UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:3];
// Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:newPosition];
// Set new range
[textField setSelectedTextRange:newRange];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文