如何在 FPNTextField 中检测国家代码和更改标志
Posted
技术标签:
【中文标题】如何在 FPNTextField 中检测国家代码和更改标志【英文标题】:How to detect the country code and change flag in FPNTextField 【发布时间】:2020-09-10 10:05:41 【问题描述】:我正在使用FlagPhoneno Pod 使用以下代码搜索或选择带有标志的国家代码。但是当谈到编辑部分时,我得到了 FPNtextFeild ex-+971568573570 的完整电话号码,默认情况下我将国家代码设置为 .AE
我想在任何其他国家/地区电话无法从 json let recivedPhoneNo = self.json["phone_number"].string ?? ""
获取时检测并自动更改国家/地区标志
目前,仅选择时标志正在更改。请向我展示一个示例方法,其中包含来自同一 pod“FlagPhoneno”模块的代码。
我正在使用以下代码。
func setupPhoneTF()
self.phoneTFView.layer.borderColor = UIColor.appColor.customGray.cgColor
self.phoneTFView.layer.cornerRadius = 5
self.phoneTFView.layer.borderWidth = 1
phoneTF.displayMode = .list // .picker by default
listController.setup(repository: phoneTF.countryRepository)
listController.didSelect = [weak self] country in
self?.phoneTF.setFlag(countryCode: country.code)
phoneTF.delegate = self
phoneTF.font = UIFont.systemFont(ofSize: 14)
// Custom the size/edgeInsets of the flag button
phoneTF.flagButtonSize = CGSize(width: 35, height: 35)
phoneTF.flagButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
let items = [
UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.save, target: self, action: nil),
UIBarButtonItem(title: "Item 1", style: .plain, target: self, action: nil),
UIBarButtonItem(title: "Item 2", style: .plain, target: self, action: nil)
]
phoneTF.textFieldInputAccessoryView = getCustomTextFieldInputAccessoryView(with: items)
phoneTF.hasPhoneNumberExample = true
phoneTF.placeholder = "Phone Number"
phoneTF.setFlag(countryCode: .AE)
extension AddNewAddressVC: FPNTextFieldDelegate
func fpnDidValidatePhoneNumber(textField: FPNTextField, isValid: Bool)
textField.rightViewMode = .whileEditing
textField.rightView = UIImageView(image: isValid ? imageLiteral(resourceName: "success") : imageLiteral(resourceName: "error"))
print(
isValid,
textField.getFormattedPhoneNumber(format: .E164) ?? "E164: nil",
textField.getFormattedPhoneNumber(format: .International) ?? "International: nil",
textField.getFormattedPhoneNumber(format: .National) ?? "National: nil",
textField.getFormattedPhoneNumber(format: .RFC3966) ?? "RFC3966: nil",
textField.getRawPhoneNumber() ?? "Raw: nil"
)
func fpnDidSelectCountry(name: String, dialCode: String, code: String)
print(name, dialCode, code)
self.selectedDialCod = dialCode
func fpnDisplayCountryList()
let navigationViewController = UINavigationController(rootViewController: listController)
listController.title = "Countries"
listController.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .stop, target: self, action: #selector(dismissCountries))
self.present(navigationViewController, animated: true, completion: nil)
【问题讨论】:
【参考方案1】://您可以更改选择的标志,然后设置电话号码
phoneNumberTextField.setFlag(for: .FR)
phoneNumberTextField.set(phoneNumber: "0600000001")
// 或者直接设置带有国家代码的电话号码,会自动更新国旗图片
phoneNumberTextField.set(phoneNumber: "+33600000001")
【讨论】:
以上是关于如何在 FPNTextField 中检测国家代码和更改标志的主要内容,如果未能解决你的问题,请参考以下文章