ios - 如何检测国家代码并更改 FPNTextField 中的标志

标签 ios swift uitextfield country-codes

我正在使用FlagPhoneno Pane 可使用以下代码搜索或选择带有标志的国家/地区代码。但是当谈到编辑部分时,我得到了 FPNtextFeild 的完整电话号码,前 +971568573570,默认情况下我将国家/地区代码设置为 .AE

当任何其他国家/地区电话无法从 json 获取时,我想检测并自动更改国家标志 let receivePhoneNo = 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)
    }
}

最佳答案

//您可以更改所选标志然后设置电话号码

phoneNumberTextField.setFlag(for: .FR)
phoneNumberTextField.set(phoneNumber: "0600000001")

//或者直接设置电话号码带国家代码,会自动更新国旗图片

phoneNumberTextField.set(phoneNumber: "+33600000001")

关于ios - 如何检测国家代码并更改 FPNTextField 中的标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63827351/

相关文章:

ios - 屏幕顶部边缘的自定义拖动 View 与通知中心冲突

ios - 推送通知在 IOS 11 中不起作用

ios - 检查后尝试使用现有主键创建对象的 RLMException

swift - 无法将类型“UITextField”与预期参数类型 ‘String’ 协调一致

ios - Swift UITextField 填充顶部

ios - 如何使用 Alamofire 上传带有身份验证的 MultipartFormData

iphone - 将设备添加到配置文件后是否必须重新存档项目?

ios - 单击其他选项卡栏项目时,自定义选项卡栏会调整大小

ios - 一个 ViewController 中存在多个 UIPickerView

ios - 在 Xcode UI Test 中,如何重复检查元素是否存在以及如果存在则执行操作?