SKCountryPicker 4.0.0

SKCountryPicker 4.0.0

CountryPicker 维护。




  • 作者:
  • SuryaKant Sharma

CountryPicker

CountryPicker

Awesome Travis CI codecov Version Carthage compatible Swift Package Manager compatible License

一个简单的、可定制的国家选择器,用于选择国家或拨打代码。

本库用于许多应用程序中选择用户的国家代码。用户可以通过搜索然后在列表中选择国家来选择国家。

如果您喜欢 CountryPicker,请在此页面的右上角给它一个星。

功能

  • 通过搜索和章节标题索引浏览(例如在 iOS 的“联系人”应用程序中)
  • 自动滚动到之前选定的国家
  • 过滤国家选项
  • 样式化视图选项
  • 优化图像大小
  • 集成到 Cocoa Pods
  • 集成到 Carthage
  • 集成到 Swift 包管理器
  • 遵循最佳实践
  • 支持 iOS 13 的深色模式
  • 支持 Dynamic font size for ContentSizeCategory
  • 单元测试覆盖率 94%
  • 添加了带有自定义的 Picker视图支持
  • Swift UI 支持以及示例项目

需求

  • 支持iOS 11.0+及最新版本iOS 16
  • 最新Xcode 14.x及Swift

示例项目

要运行示例项目,首先克隆存储库,然后从“示例”目录运行pod update。

截图

主页界面 地区选择界面 过滤界面 深色模式界面 选择器视图

安装

CountryPicker可通过CocoaPods和Carthage获得。

CocoaPods

将以下行添加到您的Podfile中

pod 'SKCountryPicker'

当前版本与 Swift 5 兼容。如果您想支持 Swift 4.1/3.3

pod 'SKCountryPicker' '~> 2.0.0'

Carthage

使用 Carthage 进行依赖管理的步骤描述如下 这里,但出于谨慎考虑,我们也将它们添加到这个 README 中。

首先,您需要将以下行添加到您的 Cartfile

github "SURYAKANTSHARMA/CountryPicker"

以包含 CountryPicker 的最新版本。

(如果您还没有 Cartfile,您需要先在您喜欢的文本编辑器中创建一个)

接下来运行

carthage update --platform iOS

这将使 Carthage

  • 从 Github 获取 CountryPicker 的源代码
  • 将源代码编译成一个框架以供您使用

一旦 Carthage 编译完成,您需要将框架添加到您的项目中。

在 Xcode 中

  • 导航到项目的“通用”选项卡,点击“框架、库和嵌入内容”下的加号
  • 选择“添加其他”,然后定位到 Carthage 文件夹(通常位于项目的根目录)
  • SKCountryPicker.framework 位于 Carthage/Build/iOS/
  • 选择它并确认它已被添加为框架。

最后一步是将 copy-frameworks 构建脚本添加到您的“构建阶段”中。

  • 导航到“构建阶段”,然后点击顶部的加号
  • 选择“新运行脚本阶段”
  • 将以下行作为要运行的脚本粘贴 /usr/local/bin/carthage copy-frameworks
  • 像这样添加 SKCountryPicker.framework 到输入文件: $(SRCROOT)/Carthage/Build/iOS/SKCountryPicker.framework

完成!

SPM

将以下行添加到 Package.swift 文件中的 dependencies 部分

.package(url: "https://github.com/SURYAKANTSHARMA/CountryPicker.git, from "1.2.7")

入门指南

示例:请检查 示例 项目以了解自定义和不同的选项。

    func presentCountryPickerScene(withSelectionControlEnabled selectionControlEnabled: Bool = true) {
        switch selectionControlEnabled {
        case true:
            // Present country picker with `Section Control` enabled
            CountryPickerWithSectionViewController.presentController(on: self, configuration: { countryController in
                countryController.configuration.flagStyle = .circular
                countryController.configuration.isCountryFlagHidden = !showCountryFlagSwitch.isOn
                countryController.configuration.isCountryDialHidden = !showDialingCodeSwitch.isOn
                countryController.favoriteCountriesLocaleIdentifiers = ["IN", "US"]

            }) { [weak self] country in
                
                guard let self = self else { return }
                self.countryImageView.isHidden = false
                self.countryImageView.image = country.flag
                self.countryCodeButton.setTitle(country.dialingCode, for: .normal)
            }
            
        case false:
            // Present country picker without `Section Control` enabled
            CountryPickerController.presentController(on: self, configuration: { countryController in
                countryController.configuration.flagStyle = .corner
                countryController.configuration.isCountryFlagHidden = !showCountryFlagSwitch.isOn
                countryController.configuration.isCountryDialHidden = !showDialingCodeSwitch.isOn
                countryController.favoriteCountriesLocaleIdentifiers = ["IN", "US"]

            }) { [weak self] country in
                
                guard let self = self else { return }
                
                self.countryImageView.isHidden = false
                self.countryImageView.image = country.flag
                self.countryCodeButton.setTitle(country.dialingCode, for: .normal)
            }
        }
    }
}

过滤选项

有3个主要的过滤选项 countryNamecountryCodecountryDialCode,默认情况下,国家选择器已经配置为根据 countryName 过滤国家。

如果您想添加或删除过滤选项,请按照以下步骤操作

 // Adding filter
 CountryManager.shared.addFilter(.countryCode)

 // Removing filter
 CountryManager.shared.removeFilter(.countryCode)

 // Removing all filters
 CountryManager.shared.clearAllFilters()

如果您想获取 country 信息

// Get country based on digit code e.g: 60, +255
CountryManager.shared.country(withDigitCode: "255")

// Get country based on country name
CountryManager.shared.country(withName: "Tanzania")

// Get country based on country code e.g: MY, TZ
CountryManager.shared.country(withCode: "MY")

样式选项

库提供了一些样式选项,例如自动隐藏或样式视图。

CountryPickerWithSectionViewController.presentController(on: self, configuration: { controller in
    // Styling country flag image view
    controller.configuration.flagStyle = .circular 
    
    // Hide flag image view
    controller.configuration.isCountryFlagHidden = true
    
    // Hide country dial code
    controller.configuration.isCountryDialHidden = true
 })

依赖图

Dependecy graph

贡献

任何使项目变得更好的贡献都受到欢迎。

作者

*Surayakant Sharma

请参阅参与此项目的其他贡献者名单。衷心感谢 Hardeep Singh 提供的灵感。

许可证

本项目遵循MIT许可证 - 有关详细信息,请参阅LICENSE文件。