一个简单的 Swift 实现的 Material Design 轮廓文本字段。
可以使用 Swift Package Manager 进行安装。 在 Xcode 中,转到 File > Swift Packages > Add Package Dependency… 并粘贴存储库 URL (https://github.com/flowbe/MaterialOutlinedTextField) 来添加它。
您也可以直接在您的 Package.swift
文件中添加依赖项
dependencies: [
.package(url: "https://github.com/flowbe/MaterialOutlinedTextField.git", .upToNextMajor(from: "0.1.0"))
]
MaterialOutlinedTextField
具有与 UITextField
相同的接口,但增加了一些额外的属性和方法
label
: 作为浮动标签或占位符出现的标签。 您可以使用此属性设置其文本内容。labelBehavior
: 定义文本字段处于编辑状态时标签的行为。 可能的值为 floats
(默认)或 disappears
。containerRadius
: 文本字段的圆角半径。colorModel
: 基于当前状态的当前颜色模型 (只读)。outlineLineWidth
: 基于当前状态的当前轮廓线宽度模型 (只读)。setColorModel(_ colorModel: ColorModel, for state: State)
: 为指定状态设置颜色模型。setOutlineLineWidth(_ outlineLineWidth: CGFloat, for state: State)
: 为指定状态设置颜色模型。let t = MaterialOutlinedTextField(frame: CGRect(x: 0, y: 0, width: 200, height: 56))
textField.label.text = "Label"
textField.placeholder = "Placeholder"
textField.clearButtonMode = .whileEditing
textField.setColorModel(ColorModel(textColor: .gray, floatingLabelColor: .gray, normalLabelColor: .gray, outlineColor: .gray), for: .normal)
textField.setColorModel(ColorModel(textColor: .systemBlue, floatingLabelColor: .systemBlue, normalLabelColor: .systemBlue, outlineColor: .systemBlue), for: .editing)
textField.setColorModel(ColorModel(with: .disabled), for: .disabled)