【发布时间】:2018-09-29 23:50:12
【问题描述】:
我想在 UITextField placeHolder 文本之间添加空格。
func setTextFieldCharacterSpacing(txtField: TextFieldCustomize) {
let attributedString = NSMutableAttributedString(string: txtField.text!)
attributedString.addAttribute(NSAttributedStringKey.kern, value: CGFloat(8.0), range: NSRange(location: 0, length: attributedString.length))
txtField.attributedText = attributedString
}
编辑 1
我已经通过使用textField delegate 方法完成了此操作,如下所示。
extension LoginVC: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
CommonHelper.sharedInstance.setTextFieldCharacterSpacing(txtField: textField as! TextFieldCustomize)
return true
}
}
编辑 2
我有 1 个textField 可以安全进入。当我在 textField 中键入任何内容时,txtField.attributedText未应用字符之间的空格。
使用安全字段
这适用于Username,但不适用于password 安全字段。
如何在安全条目textField中的字符之间添加空格?
提前致谢。
【问题讨论】:
-
你为什么不用
attributedPlaceholder? -
因为我还想在占位符中的字符之间添加空格。
-
你的代码的输出是什么?
-
在我的问题中添加的代码对于字符之间的空格可以正常工作。但在将此代码用于
attributedPlaceholder时遇到错误。 -
具有文本的安全输入字段,其他文件具有占位符。
标签: swift uitextfield nsattributedstring placeholder