如何确定UITextField中删除的位置

时间:2020-12-14 15:04:05

I'm creating a text field that allows the user to enter a date, and I want to show the format required: mm-dd-yyyy. As they type, it should replace the format with the numbers they type. For instance, if they enter 125, it should look like: 12-5d-yyyy.

我正在创建一个允许用户输入日期的文本字段,我想显示所需的格式:mm-dd-yyyy。在键入时,它应该用他们键入的数字替换格式。例如,如果他们输入125,它应该看起来像:12-5d-yyyy。

I've been able to get this working (using the textField:shouldChangeCharactersInRange:replacementString: method). But there are 2 problems:

我已经能够使用它(使用textField:shouldChangeCharactersInRange:replacementString:方法)。但是有两个问题:

  1. When I update the text field so that it shows the format plus what they have typed (by directly setting textField.text) the cursor goes to the end of the inserted text. For example, it currently looks like: 12-30-yyyy| (where | is the cursor), but I want it to look like 12-30-|yyyy. So how can I place the cursor where they last typed?

    当我更新文本字段以便它显示格式加上他们键入的内容时(通过直接设置textField.text),光标将转到插入文本的末尾。例如,它目前看起来像:12-30-yyyy | (其中|是光标),但我希望它看起来像12-30- | yyyy。那么如何将光标放在最后输入的位置呢?

  2. I have not been able to determine where a deletion occurs if the user presses backspace or delete. The only way I know to determine that they pressed backspace or delete is like this: BOOL thisIsBackspace = ([string length] == 0) (where string is the value of replacementString:. But this doesn't tell me where it occurred. So how can I determine where a deletion occurs in UITextField?

    如果用户按退格键或删除,我无法确定删除的位置。我知道确定他们按下退格键或删除的唯一方法是这样的:BOOL thisIsBackspace =([string length] == 0)(其中string是replacementString:的值。但是这并没有告诉我它在哪里发生。那么如何确定UITextField中删除的位置?

2 个解决方案

#1


1  

Using the UIDatePicker would be the way to go. Aside from that...

使用UIDatePicker将是最佳选择。除此之外......

In textField:shouldChangeCharactersInRange: the range parameter will tell you where where in the field the actual change is occurring.

在textField:shouldChangeCharactersInRange中:range参数将告诉您实际更改在字段中的位置。

You can also use stringByReplacingCharactersInRange to create the value of the field after edit. Then you can use it to compare and find where they edited.

您还可以使用stringByReplacingCharactersInRange在编辑后创建字段的值。然后,您可以使用它来比较和查找他们编辑的位置。

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];

    // Now you can compare text and textField.text and find where they are different.

    return YES;
}

#2


0  

You can place the cursor by using:

您可以使用以下方法放置光标:

[textField setSelectedRange:NSMakeRange(desiredPosition, 0)];

You can determine where the deletion was made by using the "range" input for the method

您可以使用方法的“范围”输入来确定删除的位置

textField:shouldChangeCharactersInRange:replacementString:

#1


1  

Using the UIDatePicker would be the way to go. Aside from that...

使用UIDatePicker将是最佳选择。除此之外......

In textField:shouldChangeCharactersInRange: the range parameter will tell you where where in the field the actual change is occurring.

在textField:shouldChangeCharactersInRange中:range参数将告诉您实际更改在字段中的位置。

You can also use stringByReplacingCharactersInRange to create the value of the field after edit. Then you can use it to compare and find where they edited.

您还可以使用stringByReplacingCharactersInRange在编辑后创建字段的值。然后,您可以使用它来比较和查找他们编辑的位置。

- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];

    // Now you can compare text and textField.text and find where they are different.

    return YES;
}

#2


0  

You can place the cursor by using:

您可以使用以下方法放置光标:

[textField setSelectedRange:NSMakeRange(desiredPosition, 0)];

You can determine where the deletion was made by using the "range" input for the method

您可以使用方法的“范围”输入来确定删除的位置

textField:shouldChangeCharactersInRange:replacementString: