FlagPhoneNumber
FlagPhoneNumber 是一个带有花哨国家代码选择器的电话号码文本框。
🌅 截图
🌍 本地化
国家名称将根据电话语言显示
📲 示例
要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
。
⬇️ 安装
FlagPhoneNumber 通过 CocoaPods 和 Carthage 提供。
Carthage
只需在 Cartfile
中添加以下行:
github "chronotruck/FlagPhoneNumber"
Cocoapods
只需在 Podfile
中添加以下行:
pod "FlagPhoneNumber"
Manually
只需在您的项目中导入 Sources 仓库。
👴🏻 Objective-C 支持
🗺 使用方法
您可以在故事板/xibs中实例化它,也可以通过编程方式进行
let phoneNumberTextField = FPNTextField(frame: CGRect(x: 0, y: 0, width: view.bounds.width - 16, height: 50))
// You can change the chosen flag then set the phone number
phoneNumberTextField.setFlag(for: .FR)
phoneNumberTextField.set(phoneNumber: "0600000001")
// Or directly set the phone number with country code, which will update automatically the flag image
phoneNumberTextField.set(phoneNumber: "+33600000001")
🚨 FPNTextFieldDelegate
FPNTextFieldDelegate 继承自 UITextFieldDelegate,因此没有变化
phoneNumberTextField.delegate = self
它提供了三个方法
extension YourViewController: FPNTextFieldDelegate {
/// The place to present/push the listController if you choosen displayMode = .list
func fpnDisplayCountryList() {
let navigationViewController = UINavigationController(rootViewController: listController)
present(navigationViewController, animated: true, completion: nil)
}
/// Lets you know when a country is selected
func fpnDidSelectCountry(name: String, dialCode: String, code: String) {
print(name, dialCode, code) // Output "France", "+33", "FR"
}
/// Lets you know when the phone number is valid or not. Once a phone number is valid, you can get it in severals formats (E164, International, National, RFC3966)
func fpnDidValidatePhoneNumber(textField: FPNTextField, isValid: Bool) {
if isValid {
// Do something...
textField.getFormattedPhoneNumber(format: .E164), // Output "+33600000001"
textField.getFormattedPhoneNumber(format: .International), // Output "+33 6 00 00 00 01"
textField.getFormattedPhoneNumber(format: .National), // Output "06 00 00 00 01"
textField.getFormattedPhoneNumber(format: .RFC3966), // Output "tel:+33-6-00-00-00-01"
textField.getRawPhoneNumber() // Output "600000001"
} else {
// Do something...
}
}
}
🎨 自定义
默认情况下,选择器视图会显示,但您也可以使用列表视图控制器显示国家
var listController: FPNCountryListViewController = FPNCountryListViewController(style: .grouped)
phoneNumberTextField.displayMode = .list // .picker by default
listController.setup(repository: textField.countryRepository)
listController.didSelect = { [weak self] country in
self?.textField.setFlag(countryCode: country.code)
不要忘记实现 fpnDisplayCountryList
func fpnDisplayCountryList() {
let navigationViewController = UINavigationController(rootViewController: listController)
listController.title = "Countries"
self.present(navigationViewController, animated: true, completion: nil)
}
默认使用 FlagKit,但您可以使用自己的国旗图标资产自定义列表
// Be sure to set it before initializing a FlagPhoneNumber instance.
Bundle.FlagIcons = YOUR_FLAG_ICONS_BUNDLE
您可以更改国旗按钮的大小
phoneNumberTextField.flagButtonSize = CGSize(width: 44, height: 44)
您可以自定义 textfield 的输入辅助视图
phoneNumberTextField.textFieldInputAccessoryView = getCustomTextFieldInputAccessoryView(with: items)
您还可以自定义国旗按钮的属性
// This will freeze the flag.
// Only one particular country's phone numbers will be formatted and validated.
// You can set the country by setting the flag as shown earlier.
phoneNumberTextField.flagButton.isUserInteractionEnabled = false
您可以在占位符中提供所选国家的电话号码示例,或者使用您自己的占位符
phoneNumberTextField.hasPhoneNumberExample = false // true by default
phoneNumberTextField.placeholder = "Phone Number"
您可以选择要在列表中显示哪些国家
phoneNumberTextField.setCountries(including: [.FR, .ES, .IT, .BE, .LU, .DE])
或者从列表中排除某些国家
phoneNumberTextField.setCountries(excluding: [.AM, .BW, .BA])
您可以选择在选择器中显示国家电话代码,或者在选择视图控制器中显示
phoneNumberTextField.pickerView.showCountryPhoneCode = false // true by default
listController.showCountryPhoneCode = false // true by default
您可以根据需要复用 FPN
类!
☕️ 概念
此库使用 libPhoneNumber-iOS 库 (https://github.com/iziz/libPhoneNumber-iOS)
开源时间由 Chronotruck 高兴赞助。
💁🏻♂️ 作者
grifas, [email protected]
不要犹豫联系我或发起拉取请求以升级此库。
📝 许可证
FlagPhoneNumber 在 Apache 许可证下可用。有关更多信息,请参阅 LICENSE 文件。