KSTokenView 4.0.1

KSTokenView 4.0.1

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2017年11月
SwiftSwift 版本4.0
SPM支持 SPM

Khawar Shahzad 维护。




  • Khawar Shahzad

KSTokenView

KSTokenView 是一种控件,它在一个可编辑的 UITextField 中显示一系列标记,并向代理对象发送消息。它还可以用于从用户处接收文本并异步执行搜索操作。UI 可以完全自定义,以看起来像是应用程序的一部分。

演示

点击 此处 进行实时预览。

垂直

gif1

水平

gif2

屏幕截图

iphone1
iphone2
iphone3
iphone4

需求

  • iOS 9 及以上。
  • Xcode 8.1 及以上
  • Swift 4 及以上
    (对于 Swift 2.x 使用 develop/swift2.3 分支)
    (对于 Swift 3.x 使用 develop/swift3 分支)

将 KSTokenView 添加到您的项目

方法 1: (仅适用于 iOS 9.0 及以上版本的 CocoaPods)

在您的 Podfile 中为 KSTokenView 添加 pod 条目

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'KSTokenView', '~> 4.0'
end

然后,运行以下命令

$ pod install

方法 2: (源文件)

或者,您可以直接将 KSTokenView 目录下的所有源文件直接添加到您的项目中。

  1. 下载最新的代码版本 [最新代码版本],或将仓库作为 git 子模块添加到您的已跟踪项目。
  2. 打开您的 Xcode 项目,然后拖动 KSTokenView 目录到您的项目中。如果您在项目外提取了代码存档,请确保选择“复制项”。

使用方法

Interface Builder

  • 从 Interface Builder 中,将 UIView 拖放到您的 View 中。
  • 身份检查器 中,设置自定义类 KSTokenView
  • 在您的源文件中创建一个 outlets
  • 自定义属性并实现代理。
tokenView.delegate = self
tokenView.promptText = "Top 5: "
tokenView.placeholder = "Type to search"
tokenView.descriptionText = "Languages"
tokenView.maxTokenLimit = 5 //default is -1 for unlimited number of tokens
tokenView.style = .Squared`

编程方式

编程方式创建 KSTokenView 对象并将其作为 Subview 添加。

let tokenView = KSTokenView(frame: CGRect(x: 10, y: 50, width: 300, height: 40))
tokenView.delegate = self
tokenView.promptText = "Top 5: "
tokenView.placeholder = "Type to search"
tokenView.descriptionText = "Languages"
tokenView.maxTokenLimit = 5
tokenView.style = .Squared
view.addSubview(tokenView)

自定义

按照以下方式自定义 KSTokenView 的外观

/// default is true. token can be deleted with keyboard 'x' button
tokenView.shouldDeleteTokenOnBackspace = true

/// Only works for iPhone now, not iPad devices. default is false. If true, search results are hidden when one of them is selected
tokenView.shouldHideSearchResultsOnSelect = false

/// default is false. If true, already added token still appears in search results
tokenView.shouldDisplayAlreadyTokenized = false

/// default is ture. Sorts the search results alphabatically according to title provided by tokenView(_:displayTitleForObject) delegate
tokenView.shouldSortResultsAlphabatically = true

/// default is true. If false, token can only be added from picking search results. All the text input would be ignored
tokenView.shouldAddTokenFromTextInput = true

/// default is 1
tokenView.minimumCharactersToSearch = 1

/// Default is (TokenViewWidth, 200)
tokenView.searchResultSize = CGSize(width: tokenView.frame.width, height: 120)

/// Default is whiteColor()
tokenView.searchResultBackgroundColor = UIColor.whiteColor()

/// default is UIColor.blueColor()
tokenView.activityIndicatorColor = UIColor.blueColor()

/// default is 120.0. After maximum limit is reached, tokens starts scrolling vertically
tokenView.maximumHeight = 120.0

/// default is UIColor.grayColor()
tokenView.cursorColor = UIColor.grayColor()

/// default is 10.0. Horizontal padding of title
tokenView.paddingX = 10.0

/// default is 2.0. Vertical padding of title
tokenView.paddingY = 2.0

/// default is 5.0. Horizontal margin between tokens
tokenView.marginX = 5.0

/// default is 5.0. Vertical margin between tokens
tokenView.marginY = 5.0

/// default is UIFont.systemFontOfSize(16)
tokenView.font = UIFont.systemFontOfSize(16)

/// default is 50.0. Caret moves to new line if input width is less than this value
tokenView.minWidthForInput = 100.0

/// default is ", ". Used to seperate titles when untoknized
tokenView.seperatorText = ", "

/// default is 0.25.
tokenView.animateDuration = 0.25

/// default is true. When resignFirstResponder is called tokens are removed and description is displayed.
tokenView.removesTokensOnEndEditing = true

/// Default is "selections"
tokenView.descriptionText = "Languages"

/// set -1 for unlimited.
tokenView.maxTokenLimit = 5

/// default is "To: "
tokenView.promptText = "Top 5: "

/// default is true. If false, cannot be edited
tokenView.editable = true

/// default is nil
tokenView.placeholder = "Type to search"

/// default is .Rounded, creates rounded corner
tokenView.style = .Squared

/// default is .Vertical, following creates horizontal scrolling direction
tokenView.direction = .Horizontal

/// An array of string values. Default values are "." and ",". Token is created with typed text, when user press any of the character mentioned in this Array
tokenView.tokenizingCharacters = [","]

请参阅示例项目以获取详细信息。

许可证

此代码根据 MIT 许可证条款和条件进行分发。