我想在Swift的Material Components Textfield中添加一个显示/隐藏文本按钮。我确实要注意,由于对Material Components的不良,不清楚和不直观的使用,使其在Swift中不是最佳选择,并降低了整体代码质量。但是,我必须使用它。我已经用一个小时的时间在Google上进行了搜索,因此,如果有人可以简单地解释一下,那就太好了。这是我当前的代码:
@IBOutlet weak var usernameTextField: MDCTextField! {
didSet {
usernameController = MDCTextInputControllerOutlined(textInput: usernameTextField)
let colorScheme = MDCSemanticColorScheme()
colorScheme.primaryColor = UIColor(named: "ECH Blue")!
colorScheme.onSurfaceColor = UIColor(named: "ECH Dark Grey")!
let containerScheme = MDCContainerScheme()
containerScheme.colorScheme = colorScheme
usernameController?.applyTheme(withScheme: containerScheme)
usernameTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
usernameTextField.delegate = self
}
}
@IBOutlet weak var passwordTextField: MDCTextField! {
didSet{
passwordController = MDCTextInputControllerOutlined(textInput: passwordTextField)
let colorScheme = MDCSemanticColorScheme()
colorScheme.primaryColor = UIColor(named: "ECH Blue")!
colorScheme.onSurfaceColor = UIColor(named: "ECH Dark Grey")!
let containerScheme = MDCContainerScheme()
containerScheme.colorScheme = colorScheme
passwordController?.applyTheme(withScheme: containerScheme)
passwordTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: UIControl.Event.editingChanged)
passwordTextField.isSecureTextEntry = true
passwordTextField.delegate = self
}
}
请注意,此示例的实现不佳的github:https://github.com/material-components/material-components-ios-codelabs/blob/master/MDC-101/Swift/Complete/Shrine/Shrine/LoginViewController.swift不能执行此操作,并且存在错误。