gpt4 book ai didi

ios - UITextfield 文本越界

转载 作者:可可西里 更新时间:2023-11-01 01:59:55 25 4
gpt4 key购买 nike

如何防止文本字段的文本超出其可显示 范围?

enter image description here

我试图使文本字段的宽度随着其中文本的增长而增长,但文本字段的文本仍然超出范围。

最佳答案

试试这个:

func getWidth(text: String) -> CGFloat {
let txtField = UITextField(frame: .zero)
txtField.text = text
txtField.sizeToFit()
return txtField.frame.size.width
}

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let txtFieldWidth = getWidth(textField.text!)

if UIScreen.mainScreen().bounds.width - 100 > txtFieldWidth {
txtWidthOfName.constant = 0.0
if txtFieldWidth > txtWidthOfName.constant {
txtWidthOfName.constant = txtFieldWidth
}
self.view.layoutIfNeeded()
}
return true
}

关于ios - UITextfield 文本越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47493357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com