我可以在 UITextField 中选择特定的文本块吗?

发布于 2024-09-10 13:14:37 字数 127 浏览 5 评论 0原文

我的 iPhone 应用程序中有一个 UITextField。我知道如何使文本字段选择其所有文本,但如何更改选择?假设我想选择最后 5 个字符或特定范围的字符,这可能吗?如果没有,我可以移动标记选择的开始和结束的线,就像用户拖动它们一样吗?

I have a UITextField in my iPhone app. I know how to make the text field select all of its text, but how can change the selection? Say I wanted to select the last 5 characters, or a specific range of characters, is that possible? if not, can I move the lines marking the beginning and end of the selection, as if the user is dragging them?

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

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

发布评论

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

评论(7

败给现实 2024-09-17 13:14:37

对于 UITextField,你不能这样做。但是,如果您看到标题,则您有 _selectedRange 和其他内容,如果您向其中添加一些类别,则可能会使用它们;)

iOS5 及更高版本的更新:

现在 UITextField 和 UITextView 符合 UITextInput 协议,因此可以:)

选择最后 5 个字符在插入符号之前会是这样的:

// 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:-5];

// Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];

// Set new range
[textField setSelectedTextRange:newRange];

With UITextField, you cannot. But if you see the headers, you have _selectedRange and others that might be used if you add some categories to it ;)

Update for iOS5 and above :

Now UITextField and UITextView conform to UITextInput protocol so it is possible :)

Selecting the last 5 characters before the caret would be like this:

// 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:-5];

// Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];

// Set new range
[textField setSelectedTextRange:newRange];
鹊巢 2024-09-17 13:14:37

要选择特定范围的字符,您可以在 iOS 5+ 中执行类似的操作,

int start = 2;
int end = 5;
UITextPosition *startPosition = [self positionFromPosition:self.beginningOfDocument offset:start];
UITextPosition *endPosition = [self positionFromPosition:self.beginningOfDocument offset:end];
UITextRange *selection = [self textRangeFromPosition:startPosition toPosition:endPosition];
self.selectedTextRange = selection;

因为 UITextField 和其他 UIKit 元素都有自己的私有子类 UITextPositionUITextRange 您不能直接创建新值,但您可以使用文本字段通过对文本开头或结尾的引用以及整数偏移量来创建新值。

您还可以执行相反的操作来获取当前选择的起点和终点的整数表示:

int start = [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.start];
int end = [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.end];

这是一个类别,它添加了使用 NSRange 处理选择的方法。 https://gist.github.com/4463233

To select a specific range of characters you can do something like this in iOS 5+

int start = 2;
int end = 5;
UITextPosition *startPosition = [self positionFromPosition:self.beginningOfDocument offset:start];
UITextPosition *endPosition = [self positionFromPosition:self.beginningOfDocument offset:end];
UITextRange *selection = [self textRangeFromPosition:startPosition toPosition:endPosition];
self.selectedTextRange = selection;

Since UITextFields and other UIKit elements have their own private subclasses of UITextPosition and UITextRange you can not create new values directly, but you can use the text field to create them for you from a reference to the beginning or end of the text and an integer offset.

You can also do the reverse to get integer representations of the start and end points of the current selection:

int start = [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.start];
int end = [self offsetFromPosition:self.beginningOfDocument toPosition:self.selectedTextRange.end];

Here is a category which adds methods to handle selections using NSRanges. https://gist.github.com/4463233

ˇ宁静的妩媚 2024-09-17 13:14:37

要选择不带文件扩展名的文件名,请使用以下命令:

-(void) tableViewCellDidBeginEditing:(UITableViewTextFieldCell*) cell
{
    NSInteger fileNameLengthWithoutExt = [self.filename length] - [[self.filename pathExtension] length];
    UITextField* textField = cell.textField;
    UITextPosition* start = [textField beginningOfDocument];
    UITextPosition* end = [textField positionFromPosition:start offset: fileNameLengthWithoutExt - 1]; // the -1 is for the dot separting file name and extension
    UITextRange* range = [textField textRangeFromPosition:start toPosition:end];
    [textField setSelectedTextRange:range];
}

To select the name of a file without the file extension, use this:

-(void) tableViewCellDidBeginEditing:(UITableViewTextFieldCell*) cell
{
    NSInteger fileNameLengthWithoutExt = [self.filename length] - [[self.filename pathExtension] length];
    UITextField* textField = cell.textField;
    UITextPosition* start = [textField beginningOfDocument];
    UITextPosition* end = [textField positionFromPosition:start offset: fileNameLengthWithoutExt - 1]; // the -1 is for the dot separting file name and extension
    UITextRange* range = [textField textRangeFromPosition:start toPosition:end];
    [textField setSelectedTextRange:range];
}
格子衫的從容 2024-09-17 13:14:37

这些都对我有用:

[UITextField selectAll:self];

和:

UITextField.selectedRange = NSMakeRange(0, 5);

但前提是文本字段是第一响应者。 (换句话说,仅当键盘显示时。)因此,您需要在任一选择方法之前加上以下内容:

[UITextField becomeFirstResponder];

如果您希望文本显示为选中状态。

These both work for me:

[UITextField selectAll:self];

and:

UITextField.selectedRange = NSMakeRange(0, 5);

but only if the text field is the first responder. (In other words, only if the keyboard is showing.) So, you need to precede either of the select methods with this:

[UITextField becomeFirstResponder];

if you want the text to appear selected.

南薇 2024-09-17 13:14:37

只是对已接受答案的一小部分补充。在 iOS 5 中,当 UITextView 的文本长度为 0(没有文本)时,以下行会使我的应用程序崩溃:

[self textRangeFromPosition:startPosition toPosition:endPosition];

一个小的解决方法是添加 length == 0 检查,如下所示:

if (textField.text && textField.text.length > 0) {
    // 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:-5];

    // Construct a new range using the object that adopts the UITextInput, our textfield
    UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];

    // Set new range
    [textField setSelectedTextRange:newRange];
}

Just a small addition to the accepted answer. In iOS 5, the following line crashes my app when UITextView's text length is 0 (has no text):

[self textRangeFromPosition:startPosition toPosition:endPosition];

A small workaround is to add a length == 0 check like:

if (textField.text && textField.text.length > 0) {
    // 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:-5];

    // Construct a new range using the object that adopts the UITextInput, our textfield
    UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];

    // Set new range
    [textField setSelectedTextRange:newRange];
}
自控 2024-09-17 13:14:37

Swift 5.0

这是我从 Panda.txt 中选择文件名 Panda 的方法

func textFieldDidBeginEditing(_ textField: UITextField) {
    // if textField.text is `Panda.txt`, offset will be 3
    let offset = String(textField.text!.split(separator: ".").last!).length
    let from = textField.position(from: textField.beginningOfDocument, offset: 0)
    let to = textField.position(from: textField.beginningOfDocument,
                                offset:textField.text!.length - (offset+1) )
    //now `Panda` will be selected
    textField.selectedTextRange = textField.textRange(from: from!, to: to!)
}

Swift 5.0

here is how I selecte file name Panda from Panda.txt

func textFieldDidBeginEditing(_ textField: UITextField) {
    // if textField.text is `Panda.txt`, offset will be 3
    let offset = String(textField.text!.split(separator: ".").last!).length
    let from = textField.position(from: textField.beginningOfDocument, offset: 0)
    let to = textField.position(from: textField.beginningOfDocument,
                                offset:textField.text!.length - (offset+1) )
    //now `Panda` will be selected
    textField.selectedTextRange = textField.textRange(from: from!, to: to!)
}

梦里泪两行 2024-09-17 13:14:37

Swift

选择最后 5 个字符:

if let newPosition = textField.positionFromPosition(textField.endOfDocument, inDirection: UITextLayoutDirection.Left, offset: 5) {

    textField.selectedTextRange = textField.textRangeFromPosition(newPosition, toPosition: textField.endOfDocument)
}

选择任意范围:

// Range: 3 to 7
let startPosition = textField.positionFromPosition(textField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: 3)
let endPosition = textField.positionFromPosition(textField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: 7)

if startPosition != nil && endPosition != nil {
    textField.selectedTextRange = textField.textRangeFromPosition(startPosition!, toPosition: endPosition!)
}

我的完整答案在此处

Swift

Select the last 5 characters:

if let newPosition = textField.positionFromPosition(textField.endOfDocument, inDirection: UITextLayoutDirection.Left, offset: 5) {

    textField.selectedTextRange = textField.textRangeFromPosition(newPosition, toPosition: textField.endOfDocument)
}

Select an arbitrary range:

// Range: 3 to 7
let startPosition = textField.positionFromPosition(textField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: 3)
let endPosition = textField.positionFromPosition(textField.beginningOfDocument, inDirection: UITextLayoutDirection.Right, offset: 7)

if startPosition != nil && endPosition != nil {
    textField.selectedTextRange = textField.textRangeFromPosition(startPosition!, toPosition: endPosition!)
}

My full answer is here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文