在用户键入时将UITextField的文本格式设置为“电话号码" [英] Formatting UITextField's text as Phone Number as user types in

查看:110
本文介绍了在用户键入时将UITextField的文本格式设置为“电话号码"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义UITextField,我正尝试将其格式化输入到电话号码的格式"作为用户类型.我要实现的目标:

I have a custom UITextField, which I am trying to 'format the input to phone number' as user types. What I want to achieve:

  • 在UITextField上添加一个前缀+符号作为第一个不能删除的字符.

  • Adding a prefixed + sign on the UITextField as the first character, that cannot be deleted.

将String变量和UITextField的文本格式设置为电话号码(+ 49 291 12345678),而不是纯数字(4929112345678)用户输入.

Format both the String variable and UITextField's text in the format of phone number nicely (+49 291 12345678), rather than plain numbers (4929112345678) as user types in.

我研究发现,没有内置方法.我还找到了易于使用的库,名为 PhoneNumberKit ,用于将输入字符串格式化为电话号码.在Playground中,它的工作方式类似于.

I researched and found out that there is no built-in method for that. I've also found easy-to-use library called PhoneNumberKit for formatting the input String to Phone number. In Playground, it works like..

let rawNumberArray = "+4929112345678"
let phoneNumbers = PartialFormatter().formatPartial(rawNumberArray)

print(phoneNumbers) // "+49 291 12345678"

请记住,库需要将String以+作为第一个正确格式化的字符.

Bare in mind that, the library needs the String to have + as the first character to format properly.

我尝试在UITextField上实现它.

I tried implementing it on my UITextField.

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    if textField == myTextField {
        let candidateString : NSString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
        let updatedTextString : NSString = PartialFormatter().formatPartial(candidateString as String)

        self.formattedPhoneNumber(updatedTextString, textField: textField)
    }
    return true
}

func formattedPhoneNumber(updatedTextString: NSString, textField: UITextField) {
    //  textField.text = "+\(updatedTextString as String)"
    print(updatedTextString)
}

如果我保留textField.text = "+\(updatedTextString as String)"的注释,则print(updatedTextString)在控制台上打印格式正确的字符串,但在UI的UITextField内,它仅显示普通的,非结构化的数字,例如"4929112345678"

If I keep textField.text = "+\(updatedTextString as String)" commented, print(updatedTextString) prints nicely formatted String on the console, BUT inside UITextField on UI, it just shows plain, unstructured numbers like "4929112345678"

如果我取消注释,事情开始变得很奇怪,UITextField开始在UI中显示重复的字符,但是用于打印的控制台日志也变得很奇怪.我之前也曾尝试清空textField.text = "",但是也没有用.

If I uncomment it, things start to get weird and UITextField starts present duplicated characters in the UI, but console log for print also gets pretty weird. I also tried emptying textField.text = "" before, but didn't work either.

我做错了什么?我缺少什么让它无法正常工作?我相信这与我有关,与图书馆本身无关.

What am I doing wrong? What am I missing that doesn't let it work fine? I believe it's related with me and not the library itself.

此外,如果您还有其他建议(或库)可以帮助我解决此问题,请与我们分享.

Also, if you have any other suggestions (or libraries) for me to overcome this problem, please share them.

推荐答案

如果要更改要显示的值,请在替换文本后返回false.这是一个示例:

If you are changing the values you want to show, should return false after replacing the text. Here is an example:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    if let text = textField.text {
        print("curText: \(text) range: \(range.location)-\(range.length) string: \(string)" )

        let candidate = (text as NSString).stringByReplacingCharactersInRange(range, withString: string)

        if(!candidate.isCorrectFormat()){
            textField.text = callYourFormatMethodHere();

            return false
        }

    }

    return true
}

您需要实现自己的isCorrectFormat()和callYourFormatMethodHere().

You need to implement your own isCorrectFormat() and callYourFormatMethodHere().

使用您的代码:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    if textField == myTextField {
        let candidateString : NSString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
        let updatedTextString : NSString = PartialFormatter().formatPartial(candidateString as String)

        self.formattedPhoneNumber(updatedTextString, textField: textField)

        return false // Return false here
    }
    return true
}

这篇关于在用户键入时将UITextField的文本格式设置为“电话号码"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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