Phone 来自 iOS 国家代码的数字格式
Phone number format from country code on iOS
我需要在 UITextField 中显示 phone 数字格式作为占位符。我该怎么做?
对于国家选择,我使用下面提到的库,它为我提供了针对用户选择国家的国旗和国家代码。
https://github.com/NikKovIos/NKVPhonePicker
选择国家后,我需要显示所选国家的 phone 号码格式,提交 phone 号码后,我必须验证 phone 号码。
我还发现第三方 (PhoneNumberKit) 受 google 的 libphonenumber 启发,但它用于验证,确实如此未针对国家/地区代码提供预期的 phone 数字格式。下面是link.
https://github.com/marmelroy/PhoneNumberKit
更新 1:
试过这个并得到一般解析器错误
let phoneNumberKit = PhoneNumberKit()
do {
let phoneNumber = try phoneNumberKit.parse("+921230123456")
}
catch {
print("Generic parser error")
}
更新二:
更新代码,仍然出现异常
let phoneNumberKit = PhoneNumberKit()
do {
let phoneNumber = try phoneNumberKit.parse("1230123456", withRegion: "FR", ignoreType: false)
let formatedNumber = phoneNumberKit.format(phoneNumber, toType: .international)
print(formatedNumber)
}
catch {
print("Generic parser error")
}
我不知道这是否是一个有效的解决方案,你可以试试这个
假设你的占位符是 012345679 我相信你可以做的是
- 创建一个变量来存储这个占位符
- 根据用户选择的国家/地区解析此占位符。
- 将解析的设置为文本字段中的占位符。
对于那些想做同样事情的人,我使用了两个不同的第 3 方来实现功能。
NKVPhoneNumber 用于 select 国家代码,我在元数据中引入 phone_format
对其进行了一些修改。一旦 select 从列表中编辑了一个国家,它 return 一个 Country
对象,其中包括 Code, Extension, Flag and format_placeholder
SHSPhoneComponent 然后使用 format_placeholder
验证格式。
import SHSPhoneComponent
import NKVPhonePicker
@IBOutlet weak var phoneTF: SHSPhoneTextField!
@IBOutlet weak var phoneFlag: NKVPhonePickerTextField!
@IBOutlet weak var lblCountryCode: UILabel!
//MARK: - NKV callback delegates
func countriesViewController(_ sender: CountriesViewController, didSelectCountry country: Country) {
//
phoneTF.formatter.setDefaultOutputPattern(country.formatPattern)
phoneTF.text = ""
phoneTF.placeholder = country.formatPatternPlaceHolder
countryCode = "+\(country.phoneExtension)"
lblCountryCode.text = countryCode
}
注意: 我已经将 NVKPhoneNumber 转换为 Swift 4.0,
我需要在 UITextField 中显示 phone 数字格式作为占位符。我该怎么做?
对于国家选择,我使用下面提到的库,它为我提供了针对用户选择国家的国旗和国家代码。
https://github.com/NikKovIos/NKVPhonePicker
选择国家后,我需要显示所选国家的 phone 号码格式,提交 phone 号码后,我必须验证 phone 号码。
我还发现第三方 (PhoneNumberKit) 受 google 的 libphonenumber 启发,但它用于验证,确实如此未针对国家/地区代码提供预期的 phone 数字格式。下面是link.
https://github.com/marmelroy/PhoneNumberKit
更新 1: 试过这个并得到一般解析器错误
let phoneNumberKit = PhoneNumberKit()
do {
let phoneNumber = try phoneNumberKit.parse("+921230123456")
}
catch {
print("Generic parser error")
}
更新二: 更新代码,仍然出现异常
let phoneNumberKit = PhoneNumberKit()
do {
let phoneNumber = try phoneNumberKit.parse("1230123456", withRegion: "FR", ignoreType: false)
let formatedNumber = phoneNumberKit.format(phoneNumber, toType: .international)
print(formatedNumber)
}
catch {
print("Generic parser error")
}
我不知道这是否是一个有效的解决方案,你可以试试这个
假设你的占位符是 012345679 我相信你可以做的是
- 创建一个变量来存储这个占位符
- 根据用户选择的国家/地区解析此占位符。
- 将解析的设置为文本字段中的占位符。
对于那些想做同样事情的人,我使用了两个不同的第 3 方来实现功能。
NKVPhoneNumber 用于 select 国家代码,我在元数据中引入 phone_format
对其进行了一些修改。一旦 select 从列表中编辑了一个国家,它 return 一个 Country
对象,其中包括 Code, Extension, Flag and format_placeholder
SHSPhoneComponent 然后使用 format_placeholder
验证格式。
import SHSPhoneComponent
import NKVPhonePicker
@IBOutlet weak var phoneTF: SHSPhoneTextField!
@IBOutlet weak var phoneFlag: NKVPhonePickerTextField!
@IBOutlet weak var lblCountryCode: UILabel!
//MARK: - NKV callback delegates
func countriesViewController(_ sender: CountriesViewController, didSelectCountry country: Country) {
//
phoneTF.formatter.setDefaultOutputPattern(country.formatPattern)
phoneTF.text = ""
phoneTF.placeholder = country.formatPatternPlaceHolder
countryCode = "+\(country.phoneExtension)"
lblCountryCode.text = countryCode
}
注意: 我已经将 NVKPhoneNumber 转换为 Swift 4.0,