SwiftPhoneFormat 1.0.0

SwiftPhoneFormat 1.0.0

Seth 维护。



  • 作者
  • Seth Arnott

Swift 电话号码格式化器

概述

此项目旨在简化 Swift 4+ 中的电话号码格式。虽然存在更快更简单的方法来实现电话号码格式化,但此仓库旨在提供高度可配置性,并支持多种语言和格式样式。(多语言支持仍在开发中,欢迎提供帮助!)

在 pod 中包含了一个示例项目,以演示用法。

用法

此 pod 将责任分解为独立的组件,以提供更多可配置性。

要使用内置的格式化功能,您只需实例化一个带有定义好的规则集的格式化器即可。

var formatter = PhoneFormatter(rulesets: PNFormatRuleset.usParethesis())

此库包含两个(更多正在开发中)预定义的规则集。您可以创建自己的规则集,以支持您所需的任何格式化行为。

注意:如果您想贡献更多规则集,请对该仓库进行分支,添加您的规则集,并使用包含您的更改的 pull request。规则集应按地区分开成文件夹,按照已内置的规则集的例子进行,每个文件包含 PNFormatRules 的扩展,以描述性的方式命名静态工厂函数。

以下是一些当前包含的规则集

// This extension defines rulesets for phone numbers within the United States
public extension PNFormatRuleset {

    // Formats phone numbers:
    //  .local: 123-4567
    //  .domestic: 123-456-7890
    //  .international: +1 234-567-8901
    static func usHyphen() -> [PhoneFormatRuleset] {
        return [
            PNFormatRuleset(.local, rules: [
                PNFormatRule(3, separator: .hyphen)
            ], maxLength: 7),
            PNFormatRuleset(.domestic, rules: [
                PNFormatRule(3, separator: .hyphen),
                PNFormatRule(6, separator: .hyphen)
            ], maxLength: 10),
            PNFormatRuleset(.international, rules: [
                PNFormatRule(0, separator: .plus),
                PNFormatRule(1, separator: .space),
                PNFormatRule(4, separator: .hyphen),
                PNFormatRule(7, separator: .hyphen)
            ], maxLength: 11)
        ]
    }

    // Formats phone numbers:
    //  .local: 123-4567
    //  .domestic: (123) 456-7890
    //  .international: +1 (234) 567-8901
    static func usParethesis() -> [PhoneFormatRuleset] {
        return [
            PNFormatRuleset(.local, rules: [
                PNFormatRule(3, separator: .hyphen)
            ], maxLength: 7),
            PNFormatRuleset(.domestic, rules: [
                PNFormatRule(0, separator: .parenthesisLH),
                PNFormatRule(3, separator: .parenthesisRH),
                PNFormatRule(3, separator: .space, priority: 1),
                PNFormatRule(6, separator: .hyphen)
            ], maxLength: 10),
            PNFormatRuleset(.international, rules: [
                PNFormatRule(0, separator: .plus),
                PNFormatRule(1, separator: .space),
                PNFormatRule(1, separator: .parenthesisLH, priority: 1),
                PNFormatRule(4, separator: .parenthesisRH),
                PNFormatRule(4, separator: .space, priority: 1),
                PNFormatRule(7, separator: .hyphen)
            ], maxLength: 11)
        ]
    }
}

如果您尝试将字符串格式化为用户在文本字段中输入电话号码时,您可以使用以下类似的 UITextFieldDelegate 函数

extension ViewController: UITextFieldDelegate {
    public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        if textField == phoneNumberField, let text = textField.text {

            // get current text + new character being entered
            var newStr = text + string

            // check if this is a backspace
            let isDeleting = (range.length > 0) && string.isEmpty
            if isDeleting == true {
                // trim last char for correct validation
                newStr.remove(at: newStr.index(before: newStr.endIndex))
            }

            // strip non-numeric characters
            let numberString = newStr.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()

            // 11 character limit
            if numberString.count > 11 {
                return false
            }

            // don't update the field if formatting hasn't changed
            let formatted = formatter.format(number: numberString)
            if newStr != formatted {
                phoneNumberField.text = formatted
                return false
            }
        }

        return true
    }

}