一个 macOS Cocoa 单行 NSTextField/NSSecureTextField,实现了浮动标签模式。 你可以在这里阅读关于浮动标签模式的信息。
我很好奇这种模式是否适用于 Mac 程序。我希望它能作为现有文本字段的基本替代品,使用 NSTextField 的 placeholderString
值作为浮动标签标题。
将 https://github.com/dagronf/DSFFloatLabelledTextControl
添加到您的项目中。
将 pod 'DSFFloatLabelledTextControl', :git => 'https://github.com/dagronf/DSFFloatLabelledTextControl'
添加到您的 podfile 中。
DSFFloatLabelledTextField
或 DSFFloatLabelledSecureTextControl
,具体取决于您是否需要安全字段。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,您就可以使用此控件。
placeholderTextSize
- 浮动标签中使用的文本大小(以 pt 为单位)placeholderSpacing
- 文本字段文本和浮动标签之间的距离(以 px 为单位)您可以指定一个代理 (floatLabelDelegate
),可以通过编程方式或通过 Interface Builder,以接收有关控件操作的附加信息。
2.0.0
: 将安全文本字段移动到单独的类中(修复了现代 Xcode 中的安全警告)1.8.0
: 添加了安全文本字段支持和代理支持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.