AutoCompleteTextField
功能
- 提供具有输入建议的 UITextField 子类
- 具有自动完成输入功能
- 用户提供了数据建议
- 启用存储智能域名
- 优化且轻量级
要求
- iOS 10.0+
- Xcode 11+
- Swift 5+
安装
CocoaPods
您可以使用 CocoaPods 通过将其添加到您的 Podfile
中来安装 AutoCompleteTextField
。
pod "AutoCompleteTextField"
Carthage
创建一个列出框架的 Cartfile
并运行 carthage bootstrap
。按照 说明 将 $(SRCROOT)/Carthage/Build/iOS/AutoCompleteTextField.framework
添加到 iOS 项目中。
github "nferocious76/AutoCompleteTextField"
手动
- 下载并将
/Pod/Classes
文件夹拖放到您的项目中。 - 恭喜您!
使用方法
// Initializing `AutoCompleteTextField`
let myTextField = AutoCompleteTextField(frame: CGRect(x: 20, y: 64, width: view.frame.width - 40, height: 40), dataSource: `YourDataSource`, delegate: `YourDelegate`)
// Setting `dataSource`, it can be set from the XCode IB like `TextFieldDelegate` in which will appear as `actfDataSource`
myTextField.dataSource = `YourDataSource`
// Setting delimiter (optional). If set, it will only look for suggestion after the delimiter
myTextField.setDelimiter("@")
// Show `AutoCompleteButton`
myTextField.showAutoCompleteButtonWithImage(UIImage(named: "checked"), viewMode: .whileEditing)
// Providing data source to get the suggestion from inputs
func autoCompleteTextFieldDataSource(_ autoCompleteTextField: AutoCompleteTextField) -> [ACTFWeightedDomain] {
return weightedDomains // AutoCompleteTextField.domainNames // [ACTFDomain(text: "gmail.com", weight: 0), ACTFDomain(text: "hotmail.com", weight: 0), ACTFDomain(text: "domain.net", weight: 0)]
}
ACTFDomain
ACTFDomain
是一个符合 Codable
协议的结构类型。用户可以存储和检索智能域名。
一个示例可以是 'gmail.com' 和 'georgetown.edu'。用户更可能有 'gmail.com' 账户,因此我们希望它在 'georgetown.edu' 之前显示,即使它在字母顺序上不在前面。
ACTFDomain
是根据其使用频率排序的。
这只是其中一个示例。手动提供建议提供了更大的灵活性,并且不会强制使用字符串数组。
// Usage
let g1 = ACTFDomain(text: "gmail.com", weight: 10)
let g2 = ACTFDomain(text: "googlemail.com", weight: 5)
let g3 = ACTFDomain(text: "google.com", weight: 4)
let g4 = ACTFDomain(text: "georgetown.edu", weight: 1)
let weightedDomains = [g1, g2, g3, g4] // [ACTFDomain]
// Storing
// store single
if g1.store(withKey: "Domain") {
print("Store success")
}
// store multiple
if ACTFDomain.store(domains: weightedDomains, withKey: "Domains") {
print("Store success")
}
// Retrieving
// retrieved single
if let domain = ACTFDomain.domain(forKey: "Domain") {
print("Retrieved: ", domain)
}
// retrieved multiple
if let domains = ACTFDomain.domains(forKey: "Domains") {
print("Retrieved: ", domains)
}
贡献
我们非常希望您能为AutoCompleteTextField
做出贡献。有关更多信息,请参阅LICENSE文件。
作者
Neil Francis Ramirez Hipona,[email protected]
关于
本项目受到 'HTAutocompleteTextField' 的启发,这是一个同样具有该功能的 Objc-C 框架。
许可证
AutoCompleteTextField 在 MIT 许可证下可用。有关更多信息,请参阅LICENSE文件。