macOS 浮动标签模式文本字段

一个 macOS Cocoa 单行 NSTextField/NSSecureTextField,实现了浮动标签模式。 你可以在这里阅读关于浮动标签模式的信息。

截图

为什么?

我很好奇这种模式是否适用于 Mac 程序。我希望它能作为现有文本字段的基本替代品,使用 NSTextField 的 placeholderString 值作为浮动标签标题。

安装

Swift Package Manager

https://github.com/dagronf/DSFFloatLabelledTextControl 添加到您的项目中。

Cocoapods

pod 'DSFFloatLabelledTextControl', :git => 'https://github.com/dagronf/DSFFloatLabelledTextControl' 添加到您的 podfile 中。

用法

Interface Builder

动态地

let field = DSFFloatLabelledTextField()
field.placeholderString = "Dynamic Created"
field.font = NSFont.systemFont(ofSize: 16)
parentView.addSubview(field)
		
/// Setup autolayout constraints etc.

自定义属性

这些控件继承自 NSTextField,因此所有 NSTextField 功能(如 Cocoa 绑定和 Interface Builder 设置)都可用。 如果您可以使用 NSTextField,您就可以使用此控件。

代理处理

您可以指定一个代理 (floatLabelDelegate),可以通过编程方式或通过 Interface Builder,以接收有关控件操作的附加信息。

截图

drawing

鸣谢

版本

许可证

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.