我有一个自定义UITextfield,它通过约束以x居中于其超级视图。它在文本的正下方还具有高度限制和漂亮的细线,该宽度与文本字段本身的宽度相同(就像我说的:custom)。
现在,我想在编辑UITextfield的内容时使其居中,将其宽度调整为字符串内容。
现在发生的是它在编辑时保持其宽度,裁剪了重叠的内容。
我的代码有点
// on textfields editing change event
@IBAction func nameFieldEditingChanged(sender: AnyObject) {
// set width of textfield to width of textstring
checkUserNameTextfieldWidth()
}
func checkUserNameTextfieldWidth(){
println("checkUserNameTextfieldWidth \(userName.text) / \(userName.frame.size.width) / \(view.bounds.width)")
// something has to happen here I guess...
}
谢谢!
最佳答案
您必须使用UITextView而不是UITextField并应用其sizeThatFits方法(请参见this response)
关于swift - 如何在编辑时自动在Swift中调整居中的UITextfield的宽度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32869894/