Earth 1.6.0

Earth 1.6.0

Leacode 维护。



Earth 1.6.0

  • leacode

Earth

Earth 是一个用于选择全球所有国家名称、区号、国旗和表情符号的库。适用于 iOS 和 MacOS

Version License Platform Carthage

       

特性

  • 支持有国旗、表情符号和区号的242个国家。
  • 支持12种语言。如果您所需的语言被覆盖了,欢迎提交PR。
  • CountryTextField类,用于从选择器视图中选择国家。
  • CountryPickerView 允许您从UITableView中选择国家。
  • 您可以通过在设置中设置颜色轻松支持iOS 13的黑暗模式。

示例

要运行示例项目,请先克隆仓库,并在 Example 目录中运行 pod install

需求

  • iOS 8.0+ / macOS 10.10+
  • Swift 5.0+

安装

CocoaPods

CocoaPods 是用于 Cocoa 项目的依赖管理器。您可以使用以下命令安装它:

$ gem install cocoapods

Earth 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中:

use_frameworks!

target '<Your Target Name>' do
pod 'Earth', '~> 1.6.0'
end

然后,运行以下命令

$ pod install

Carthage

Carthage 是一个去中心化的依赖管理器,它构建您的依赖项,并为您提供了二进制框架。

您可以使用以下命令使用 Homebrew 安装 Carthage:

$ brew update
$ brew install carthage

要使用 Carthage 在 Xcode 项目中集成 Earth,请在您的 Cartfile 中指定它

github "leacode/Earth" ~> 1.6.0

运行 carthage update 来构建框架,并将构建好的 Earth.framework 拖到您的 Xcode 项目中

Swift 包管理器

  • 对于 iOS/ MacOS
  1. 标记此存储库。
  2. 在 Xcode 中登录您的 GitHub 账户。
  3. 转到项目设置的 Swift 包标签,点击添加按钮,搜索 Earch,将其添加到您的项目中。
  4. 在 SPM 下载完源代码后,添加 Earth.framework。
  • 对于服务器端

由于 Swift 包管理器 不支持将资源绑定到框架中。因此,此方式不可用。

dependencies: [
    .package(url: "https://github.com/leacode/Earth.git", from: "1.6.0"),
]

如何使用

  • 获取国家信息(支持Mac OS和iOS)
import Earth

// get all countries 
let countries = CountryKit.countries

// get a country with country code
if let country = CountryKit.country(countryCode: "CN") {

     // use result here
     
     // get country's flag
     flagImageView.image = country.flag
     
     // get country's localized name
     countryTF.text = country.localizedName
}

  • 显示国家选择器(目前只支持iOS)
let countryPicker = CountryPickerViewController()
countryPicker.pickerDelegate = self
present(countryPicker, animated: true, completion: nil)
  • 自定义CountryTextField(目前只支持iOS)
...

var settings = Picker.Settings()
// style
settings.barStyle = UIBarStyle.default  // Set toobar style
settings.displayCancelButton = true     // show cancel button or not

// font
settings.cellFont = UIFont.systemFont(ofSize: 15.0) // set font color

// text
settings.placeholder = "choose a country"  // set a placeholder for the text view
settings.doneButtonText = "Done"           // set done button text
settings.cancelButtonText = "Cancel"       // set cancel button text

// colors
settings.toolbarColor = UIColor.blue                    // set toolbar color
settings.pickerViewBackgroundColor = UIColor.lightGray  // set background color of pickerView
settings.doneButtonColor = .white                       // set text color of done button
settings.cancelButtonColor = .purple                    // set text color of cancel button

// height
settings.rowHeight = 44.0                               // set row height

countryTF.settings = settings

或者您可以直接指定一个textView为'CountryPicker',并将它的delegate设置为pickerDelegate以处理结果。

  • 自定义CountryPickerViewController
let countryPicker = CountryPickerViewController()
countryPicker.pickerDelegate = self
        
var settings = CountryPickerViewController.Settings()

// style
settings.prefersLargeTitles = false
settings.hidesSearchBarWhenScrolling = false

// colors
settings.barTintColor = .orange
settings.cancelButtonColor = .white
settings.searchBarTintColor = .black

// texts
settings.searchBarPlaceholder = "搜索"
settings.title = "请选择国家"

// config
settings.showDialCode = true
settings.showFlags = true
settings.showEmojis = true

countryPicker.settings = settings
                
present(countryPicker, animated: true, completion: nil)

作者

李春宇

许可协议

Earth遵守MIT许可协议。更多信息请查看LICENSE文件。