gpt4 book ai didi

ios - 在 SwiftUI TextField 中实现 token

转载 作者:行者123 更新时间:2023-12-04 15:33:03 24 4
gpt4 key购买 nike

如何让 SwiftUI 中的 TextField 具有 UISearchBar 之类的标记?

我试图插入一个 UISearchBar 以便我可以使用它们,但是我失去了 TextField 和 List 之间交互的行为。

谢谢

最佳答案

最好的方法似乎是制作一个 UIViewRepresentable支柱 UISearchBar与 token 。
它有一个用于存储在数组中的搜索文本的绑定(bind),searchText .其中每个元素都有每个标记的文本。

/// Brings UISearchBar to SwiftUI. This enables using tokens for searching for iOS and macOS.
struct TokenSearchBar {
@Binding var searchText: [String]
}

extension TokenSearchBar: UIViewRepresentable {

internal func makeCoordinator() -> Coordinator {
Coordinator(self)
}

func makeUIView(context: Context) -> UISearchBar {
//Configures the Search Bar
let searchBar = UISearchBar()

searchBar.delegate = context.coordinator

return searchBar
}

func updateUIView(_ searchBar: UISearchBar, context: Context) {

if context.coordinator.searchText != searchText {
context.coordinator.searchText = searchText
}
}


/// Coordinator and delegate for the searchbar
internal class Coordinator: NSObject, UISearchBarDelegate {
var parent: TokenSearchBar

var searchBar: UISearchBar?

var searchText: [String] = [] {
didSet {
guard let searchBar = searchBar else {
return
}

//get the current search array
var tokenSearchText = searchText

let lastSearchTextItem: String

if tokenSearchText.isEmpty {
lastSearchTextItem = ""
} else {
lastSearchTextItem = tokenSearchText.removeLast()
}

//compare them with the search array
tokenSearchText.enumerated().forEach {
offset, searchString in

//Get the current tokens.
let currentTokens = searchBar.searchTextField.tokens

//Check if the number of tokens on display can be displayed with this offset. If not it will create a new token at the end.
if currentTokens.count > offset {

//Check if the token at this offset has the same object as the searchstring. If not will insert a new token on this offset.
//I will assume that all represented objects are strings
if let representedObject = currentTokens[offset].representedObject as? String, representedObject != searchString {
let newToken = UISearchToken(icon: nil, text: searchString)
newToken.representedObject = searchString

searchBar.searchTextField.tokens.insert(newToken, at: offset)
}

} else {
let newToken = UISearchToken(icon: nil, text: searchString)
newToken.representedObject = searchString

self.searchBar?.searchTextField.tokens.append(newToken)
}
}

//Trim the number of tokens to be equal to the tokenSearchText
let tokensToRemove = searchBar.searchTextField.tokens.count - tokenSearchText.count
if tokensToRemove > 0 {
searchBar.searchTextField.tokens.removeLast(tokensToRemove)
}

//make the search field text equal to lastSearchTextItem
if searchBar.text != lastSearchTextItem {
searchBar.text = lastSearchTextItem
}

if parent.searchText != self.searchText {
parent.searchText = self.searchText

}
}
}


init(_ searchBar: TokenSearchBar) {
self.parent = searchBar
}

//MARK: UISearchBarDelegate implementation
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
self.searchBar = searchBar

searchText = []
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
self.searchBar = searchBar

//add a new level to the search
if searchText.last?.trimmingCharacters(in: .whitespacesAndNewlines) != "" {
searchText.append("")
}
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.searchBar = searchBar

if self.searchText.last != nil, self.searchText[self.searchText.count - 1] != searchText {
self.searchText[self.searchText.count - 1] = searchText
} else {

var tokenText = searchBar.searchTextField.tokens.compactMap{$0.representedObject as? String}

tokenText.append(searchText)

self.searchText = tokenText

}
}
}
}

关于ios - 在 SwiftUI TextField 中实现 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60762749/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com