xcode swift - 将文本格式化为电话号码 [英] xcode swift - formatting text as phone number

查看:126
本文介绍了xcode swift - 将文本格式化为电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift创建一个iPhone应用程序。我正在尝试设置一个文本字段,在该文本字段中,用户可以输入一个自动格式化的电话号码,就像输入内置的联系人应用程序一样。我希望xcode有内置的方法来做到这一点。

I'm creating an iPhone app using Swift. I'm trying to setup a textfield in which the user can enter a phone number that automatically becomes formatted EXACTLY like it does in the built-in Contacts app as it is typed. I'm hoping xcode has built-in methods for doing this.

作为替代方案,我创建了自己的代码,可以在用户输入和退格时添加和删除括号,短划线等,但如果用户输入的话,这很快就会出现问题将光标移离输入文本的末尾。在联系人应用程序中,如果光标在括号后面移动并且用户点击退格,则不仅会删除括号,还会删除前面的数字。我不确定这是用一些内置的格式化方法完成的,或者是否有代码复制用括号,短划线等删除的文本,并读取光标的位置,然后计算新字符串应该是什么,并添加新的括号,破折号等。

As an alternative, I created my own code that would add and delete brackets, dashes, etc as the user types and backspaces, but this quickly became problematic if the user was to move the curser away from the end of the entered text. In the Contacts app, if the cursor is moved just after a bracket and the user hits backspace, it deletes not just the bracket but rather the number preceding it. I'm not sure if this is done with some built-in formatting method or if perhaps there is code that replicates the text shown with brackets, dashes, etc removed and reads the position of the cursor, then calculates what the new string should be, and adds new brackets, dashes, etc.

具体来说,我想知道:

1 )是否有内置方法将文本格式设置为与联系人应用程序中完全相同的电话号码?

1) Is there a built-in method to format text to look like a phone number exactly as is done in the Contacts app?

2)如果没有内置方法,有人能告诉我如何在光标位置读取Swift吗?

2) If there is no built-in method, can someone tell me how I can have Swift read in the cursor position?

谢谢!

推荐答案

没有内置方法可以做到这一点。这是一个使用 UITextField shouldChangeCharactersInRange 方法的实现:

There is no built-in way to do this. Here's one implementation that uses the UITextField's shouldChangeCharactersInRange method:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    if textField == phoneTextField
    {
        var newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
        var components = newString.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)

        var decimalString = "".join(components) as NSString
        var length = decimalString.length
        var hasLeadingOne = length > 0 && decimalString.characterAtIndex(0) == (1 as unichar)

        if length == 0 || (length > 10 && !hasLeadingOne) || length > 11
        {
            var newLength = (textField.text as NSString).length + (string as NSString).length - range.length as Int

            return (newLength > 10) ? false : true
        }
        var index = 0 as Int
        var formattedString = NSMutableString()

        if hasLeadingOne
        {
            formattedString.appendString("1 ")
            index += 1
        }
        if (length - index) > 3
        {
            var areaCode = decimalString.substringWithRange(NSMakeRange(index, 3))
            formattedString.appendFormat("(%@)", areaCode)
            index += 3
        }
        if length - index > 3
        {
            var prefix = decimalString.substringWithRange(NSMakeRange(index, 3))
            formattedString.appendFormat("%@-", prefix)
            index += 3
        }

        var remainder = decimalString.substringFromIndex(index)
        formattedString.appendString(remainder)
        textField.text = formattedString
        return false
    }
    else
    {
        return true
    }
}

如前所述: UITextField for Phone Number

这篇关于xcode swift - 将文本格式化为电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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