如何从文本字段中删除位置持有人文本使用谷歌材料MDCOutlinedTextField在swift?



这是我写的代码但是当用户在文本字段中但是没有文本时文本字段中仍然会出现占位符也会显示在浮动标签中当我在这个文本字段

时我想要删除
func setupGoogleMaterialTextFields(textFields: [MDCOutlinedTextField]) {
let containerShceme = MDCContainerScheme()
let colorScheme = MDCSemanticColorScheme()
colorScheme.primaryColor = UIColor.white
colorScheme.onSurfaceColor = UIColor.white
containerShceme.colorScheme = colorScheme
for textField in textFields {
textField.label.text = textField.placeholder 
textField.font = UIFont.myMediumSystemFont(ofSize: 18)
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder ?? "",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white,
    NSAttributedString.Key.font: UIFont.myMediumSystemFont(ofSize: 16)])
textField.containerRadius = 8
textField.sizeToFit()
textField.applyTheme(withScheme: containerShceme)
}
}

我已经像这样将委托分配给textField。

func setupGoogleMaterialTextFields(textFields: [MDCOutlinedTextField]) {
let containerShceme = MDCContainerScheme()
let colorScheme = MDCSemanticColorScheme()
colorScheme.primaryColor = UIColor.white
colorScheme.onSurfaceColor = UIColor.white
containerShceme.colorScheme = colorScheme
for textField in textFields {
textField.label.text = textField.placeholder 
textField.font = UIFont.myMediumSystemFont(ofSize: 18)
textField.attributedPlaceholder = NSAttributedString(string: textField.placeholder ?? "",
attributes: [NSAttributedString.Key.foregroundColor: UIColor.white,
    NSAttributedString.Key.font: UIFont.myMediumSystemFont(ofSize: 16)])
textField.containerRadius = 8
textField.sizeToFit()
textField.applyTheme(withScheme: containerShceme)
textField.delegate = self
}
}

在分配委托后执行此操作

extension youClassName: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.text?.isEmpty ?? false {
textField.placeholder = nil
}
}
}

最新更新