预览
安装
NKVPhonePicker 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod 'NKVPhonePicker'
您还可以使用 pod lib lint YourLibName
命令测试示例项目
pod try NKVPhonePicker
(别忘了更新您的 Cocoapods master 仓库)
对于 Swift 4.2 版本,请在 master
分支中使用 011f7090e69a8446aadd21531e4b83da745cd2d4
如果您仍在使用 Swift 2.x - 您可以设置(Swift_2.x 的发展已被弃用。仍然存在旧版本。)
pod 'NKVPhonePicker', :git => "https://github.com/NikKovIos/NKVPhonePicker.git", :branch => "Swift_2.x"
用法
- 将您的 UITextField 设置为 NKVPhonePickerTextField 类
- 将其 phonePickerDelegate 设置为 UIViewController,以便能够显示 CountriesViewController
- 如果遇到任何问题,请查看示例项目。
如果您需要任何功能或有错误,请提 issue。
示例
topTextField.phonePickerDelegate = self
topTextField.favoriteCountriesLocaleIdentifiers = ["RU", "ER", "JM"]
topTextField.rightToLeftOrientation = true
topTextField.shouldScrollToSelectedCountry = false
topTextField.flagSize = CGSize(width: 30, height: 50)
topTextField.enablePlusPrefix = false
// Setting initial custom country
let country = Country.countryBy(countryCode: "EG")
topTextField.currentSelectedCountry = country
// Setting custom format pattern for some countries
topTextField.customPhoneFormats = ["RU" : "# ### ### ## ##",
"GB": "## #### #########"]
// Adding programmatically
bottomTextField = NKVPhonePickerTextField(frame: CGRect(x: 0, y: 0, width: 0, height: 30))
bottomTextField.placeholder = "ex: 03123456"
bottomTextField.autocorrectionType = .no
bottomTextField.phonePickerDelegate = self
bottomTextField.keyboardType = .numberPad
bottomTextField.favoriteCountriesLocaleIdentifiers = ["LB"]
bottomTextField.layer.borderWidth = 1
bottomTextField.layer.borderColor = UIColor.white.cgColor
bottomTextField.layer.cornerRadius = 5
bottomTextField.font = UIFont.boldSystemFont(ofSize: 25)
bottomTextField.textColor = UIColor.white
bottomTextField.textFieldTextInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
bottomTextField.translatesAutoresizingMaskIntoConstraints = false
// prefilling
let source = NKVSource(countryCode: "ru")
bottomTextField.setCurrentCountryInitially = false
bottomTextField.preFillText(source: source, number: 7999432423)
注意:本库使用 Vojta Stavik 编写的 TextFieldPatternFormat
库
TODO
- 添加示例
- 最大数字计数变量(可使用自定义模式处理)
- 各国家的模式
查看我的其他作品
- https://github.com/Yummypets/YPImagePicker - 类似Instagram的图片选择器。该项目与s4cha合作
- https://github.com/NikKovIos/SDWebImage-CircularProgressView - 对SDWebImage的扩展,用于更改图片上的进度条
- https://github.com/NikKovIos/ObjectMapper_RealmSwift - 扩展为ObjectMapper添加Realm对象支持
