Earth

Earth 是一个用于选择世界各国国家名称、区号、国旗和表情符号的库。 适用于 iOS 和 MacOS。

Version License Platform Carthage

       

功能特性

示例

要运行示例项目,请克隆存储库,并首先从 Example 目录运行 pod install

要求

安装

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 将 Earth 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它。

github "leacode/Earth" ~> 1.6.0

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

Swift Package Manager

  1. Star 这个 repo.
  2. 在 Xcode 中登录您的 github 帐户。
  3. 转到项目设置中的 Swift Packages 选项卡,单击添加按钮并搜索 Earch,将其添加到您的项目中。
  4. 在 SPM 完成下载源代码后添加 Earth.framework。

由于 Swift Package Manager 不支持将资源绑定到框架中。 因此,矢量国旗在这种方式下不可用。

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

如何使用

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
}

let countryPicker = CountryPickerViewController()
countryPicker.pickerDelegate = self
present(countryPicker, animated: true, completion: nil)
...

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' 以处理结果。

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)

作者

Chunyu Li

许可

Earth 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。