一个简单的 NSSearchField,带有可本地化的、受管理的最近搜索菜单和 SwiftUI 支持。
NSSearchField
具有出色的内置机制来处理最近列表,这需要开发者提供一个菜单来提供最近列表。这个类自动提供了一个合适的可本地化的菜单结构。
DSFSearchField
继承自 NSSearchField
,因此搜索代理、动作等都可以按预期工作。
请注意,目前为了支持本地化,您需要使用下面的“直接”方法,直到 Swift 工具版本 5.3 变得更加普遍(它支持在模块中嵌入本地化)。
将 https://github.com/dagronf/DSFSearchField
添加到您的项目中。
将 DSFSearchField.swift
(和 DSFSearchField+SwiftUI.swift
如果您需要 SwiftUI 支持)复制到您的项目中。
NSSearchField
,然后将类类型更改为 DSFSearchField
。Autosave
设置为一个对您的项目唯一的字符串。let searchField = DSFSearchField(frame: rect, recentsAutosaveName: "primary-search")
每当搜索字段中的文本更改时,都会调用此回调。
searchField.searchTermChangeCallback = { [weak self] newSearchTerm in
// Do something with 'newSearchTerm'
...
}
当用户在搜索字段中按下“Return”键时,会调用此回调。
searchField.searchSubmitCallback = { [weak self] newSearchTerm in
// Do something with 'newSearchTerm'
...
}
您可以使用 stringValue
设置搜索文本,但我个人认为这在我尝试阅读和理解代码时扫描效果不佳。这个类提供了一个额外的属性 searchTerm
,这是一个更具描述性的属性名称。此属性是可绑定和可设置的。
简单地设置搜索词
searchField.searchTerm = "cats"
可绑定地观察搜索词。
searchField.addObserver(
self,
forKeyPath: #keyPath(DSFSearchField.searchTerm),
options: [.new],
context: nil)
DSFSearchField
为搜索控件提供了一个基本的 SwiftUI 界面。
DSFSearchField.SwiftUI(
text: $searchText,
placeholderText: "Search for colors…",
autosaveName: "SystemColorsSearchField"
)
搜索栏的边框样式。
控件的大小
注意:不幸的是,我无法弄清楚如何连接到 SwiftUI 的 onSubmit
视图修饰符(并且似乎所需的 Environment
值尚未公开可见)。因此,这意味着 SwiftUI 提供的默认 onSubmit
视图修饰符将不会被调用。
提供一个在搜索文本更改时调用的块。
DSFSearchField.SwiftUI(...)
.onUpdateSearchText { newValue in
Swift.print("Update with new value -> \(newValue)")
}
提供一个在搜索文本被提交时调用的块(用户在字段中按下 Return 键)。
DSFSearchField.SwiftUI(...)
.onSubmitSearchText { newValue in
Swift.print("Update with new value -> \(newValue)")
}
var body: some View {
DSFSearchField.SwiftUI(
text: $search1,
autosaveName: "Search1"
)
.onUpdateSearchText { newValue in
Swift.print("Update with new value -> \(newValue)")
}
.onSubmitSearchText { newValue in
Swift.print("Submit with new value -> \(newValue)")
}
}
MIT 许可证。您可以将其用于任何您想要的用途,只需注明我的工作。如果您在某个地方使用了它,请告诉我,我很乐意听到相关信息!
MIT License
Copyright (c) 2023 Darren Ford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.