iOS会自动在文本字段中添加连字符 [英] iOS automatically add hyphen in text field

查看:246
本文介绍了iOS会自动在文本字段中添加连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习iOS开发,并且很难搞清楚控件的各种事件。对于测试,我有一个UITextField,其中用户要输入以下格式的字符串:XXXX-XXXX-XXXX-XXXX

I'm learning iOS development and am having a hard time figuring out the various events for the controls. For a test I have a UITextField where the user is meant to input a string in the format: XXXX-XXXX-XXXX-XXXX

我希望能够检查如何字段中的文本是在每个条目之后,并查看是否需要附加连字符。我为此设置了我的IBAction函数但是当我将它分配给Value Changed事件时它什么也没做,当我在Editing Did End上设置它时它工作正常但是只有在用户退出时才会调用控制。

I want to be able to check how long the text in the field is after each entry and see if it needs to have a hyphen appended to it. I've set up my IBAction function for this but when I assign it to the "Value Changed" event it does nothing, it works fine when I set it on the "Editing Did End" but that will only call when the user exits the control.

编辑:只需添加,编辑已更改事件也会导致崩溃。我假设这是一个堆栈溢出或文本设置再次调用事件处理程序的东西。

Just to add, the "Editing Changed" event causes it to crash too. I assume this is a stack overflow or something where the setting of the text calls the event handler again.

所以简而言之,有没有办法设置一个事件处理程序每次用户在UITextField中输入一个字符?

So in short, is there any way to set an event handler for each time the user enters a character in the UITextField?

推荐答案

请注意,之前的答案严重不足。天堂禁止用户输入错误的数字,敢于尝试删除它!公平地说,海报注意到代码可能无法完美运行。但是,它甚至不会编译,所以买家当心过滤器应该已经很高了。如果你修复了编译错误并尝试了代码,你会发现你很容易得到与海报规定格式不符的输入。

Be aware the previous answer is woefully inadequate. Heaven forbid your user enter an incorrect digit and dare attempt to delete it! In fairness, the poster noted the code may not work perfectly. But then, it wouldn't even compile, so the buyer beware filter should already be high. If you fix the compile error and try the code, you'll see you can easily end up with input that does not match the poster's stated format.

这是一个解决方案我用于将文本字段限制为格式为123-456-7890的电话号码。调整其他数字格式是微不足道的。请注意使用传递的 NSRange 。顺便说一句,即使使用数字虚拟键盘也需要拒绝非数字字符,因为用户仍然可以通过硬件键盘输入非数字。

Here's a solution I've used for restricting a text field to a phone number of the format 123-456-7890. Adjusting for other numeric formats is trivial. Note the use of the passed NSRange. And BTW, rejecting non-digit characters is needed even when using a numeric virtual keyboard since users can still enter non-digits via a hardware keyboard.

另一个注释。我在第4和第7位数字的之后添加连字符,以便更容易删除数字。如果在第3位和第6位后添加,则必须处理删除悬空连字符的情况。下面的代码避免了这个用例。

One other note. I add the hyphen after the entry of the 4th and 7th digits to make the deleting of digits a bit easier. If you add after the 3rd and 6th digits, you will have to handle the case of deleting the dangling hyphen. The code below avoids that use case.

// Restrict entry to format 123-456-7890
- (BOOL)                textField:(UITextField *)textField
    shouldChangeCharactersInRange:(NSRange)range
                replacementString:(NSString *)string {

  // All digits entered
  if (range.location == 12) {
    return NO;
  }

  // Reject appending non-digit characters
  if (range.length == 0 &&
       ![[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[string characterAtIndex:0]]) {
    return NO;
  }

  // Auto-add hyphen before appending 4rd or 7th digit
  if (range.length == 0 &&
      (range.location == 3 || range.location == 7)) {
    textField.text = [NSString stringWithFormat:@"%@-%@", textField.text, string];
    return NO;
  }

  // Delete hyphen when deleting its trailing digit 
  if (range.length == 1 &&
      (range.location == 4 || range.location == 8))  {
    range.location--;
    range.length = 2;
    textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@""];
    return NO;
  }

  return YES;
}

这篇关于iOS会自动在文本字段中添加连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆