DSFPasscodeView

一个 macOS 上的密码输入框,类似于苹果的双重验证字段。



Swift Package Manager

关于

该控件由多个密码“单元格”组组成。每个单元格包含一个“字符”,您可以使用组分隔符来定义单元格组。您定义的模式提供了布局——包含 # 代表密码单元格,- 代表组分隔符。

例如,要创建一个由六个字符组成的密码,均匀地分成两组,每组三个单元格,您可以将密码模式设置为 "###-###"

.---. .---. .---.   .---. .---. .---.
|   | |   | |   |   |   | |   | |   |
|   | |   | |   |   |   | |   | |   |
`---' `---' `---'   `---' `---' `---'

此控件可以用于自动布局和手动布局的应用程序(控件内部不使用自动布局)。

请注意,此控件使用 @VIViewInvalidating,当属性值更改时,它会自动提供 NSView 无效化。(自动添加为依赖项)

特性

安装

使用 Swift Package Manager,将 https://github.com/dagronf/DSFPasscodeView 添加到您的项目中。

设置

pattern (String)

显示密码时要使用的单元格模式。 # 代表一个单元格,- 代表组间距。

唯一有效的字符是 # (单元格) 和 - (组间距)。尝试设置包含任何其他字符的模式会导致 fatalError()

示例:-

"###-###"
.---. .---. .---.   .---. .---. .---.
|   | |   | |   |   |   | |   | |   |
|   | |   | |   |   |   | |   | |   |
`---' `---' `---'   `---' `---' `---'

"####-##-###"
.---. .---. .---. .---.   .---. .---.   .---. .---. .---.
|   | |   | |   | |   |   |   | |   |   |   | |   | |   |
|   | |   | |   | |   |   |   | |   |   |   | |   | |   |
`---' `---' `---' `---'   `---' `---'   `---' `---' `---'

"##-##-#"
.---. .---.   .---. .---.   .---.
|   | |   |   |   | |   |   |   |
|   | |   |   |   | |   |   |   |
`---' `---'   `---' `---'   `---'

cellSpacing (CGFloat)

相邻单元格之间使用的间距

groupSpacing (CGFloat)

单元格组之间使用的间距

font (NSFont)

在单元格中显示字符时要使用的字体

padding (CGSize)

字符与其单元格边缘之间的内边距

edgeInsets (NSEdgeInsets)

单元格和控件边界之间的内边距

isEnabled (Bool)

启用或禁用控件(可观察)

验证

有两种验证方法

allowableCharacters (String)

此控件上的设置允许您指定一个包含控件中允许的字符的字符串。 默认情况下,这是 0123456789

characterValidatorBlock

对于更复杂的验证,您可以指定一个回调块,该回调块可用于验证每个字符

它接受一个字符串元素,并返回一个转换后的字符串元素(例如,大写),如果提供的字符对控件无效,则返回 nil。

// A validator block which allows numbers and case-insensitive A-F characters
self.passcode.characterValidatorBlock = { element in
   let validChars = "0123456789ABCDEF"
   let s = element.uppercased()         // Always check against uppercase
   if validChars.contains(s) {          // If the validChars contains the uppercased char...
       return s.first                   //  ... return the uppercased version
   }
   return nil                           // Unsupported char, ignore by returning nil
}

您可以绑定到这些成员变量,以接收控件内容更改时的更新。

isValidPasscode (Bool)

输入的密码是否为有效密码(即,所有值都已指定)

isEmpty (Bool)

是否尚未指定任何值

passcodeValue (String)

如果密码有效,则密码值作为字符串,否则为 nil。

委托 (DSFPasscodeViewHandling)

如果绑定不是您喜欢的方式,您可以附加一个委托来接收来自视图的消息。

func passcodeViewDidChange(
   _ view: DSFPasscodeView)

密码视图的内容更改时调用。

func passcodeView(
   _ view: DSFPasscodeView,                        // The passcode view
   updatedPasscodeValue passcode: String)          // The valid passcode as a string of characters

仅在密码有效且完整时调用。

func passcodeView(
   _ view: DSFPasscodeView,                        // The passcode view 
   didTryInvalidCharacter invalidChar: String?,    // The invalid character, or nil for a special key
   atIndex index: Int)                             // The passcode cell index where the attempt failed

如果用户在密码单元格中按下不支持的字符或键,则调用。

已知问题

如果您将 DSFPasscodeView 源代码文件直接复制到您的项目中,则 Designables 将按预期工作 (FB8358478)。

许可

MIT 许可证。 随意使用它,只需署名我的作品即可。 如果您在某处使用了它,请告诉我,我很乐意听到!

MIT License

Copyright (c) 2021 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.