【发布时间】:2016-01-09 10:33:23
【问题描述】:
在使用 Swift 编写的 iOS 应用程序中使用自动布局时,如何根据内容调整 textField 的大小?
当视图加载以及用户键入时,文本字段将根据需要调整大小以适应其内容。我参考了以下链接Resize textField Based On Content
但它不会在输入过程中自动调整字段大小
【问题讨论】:
-
如果您明确提到了问题中提到的链接,那么答案建议您需要使用
UITextView而不是UITextField。 -
是的,我已经使用了链接中提到的 textview..但它没有按我的意愿工作..这是我的代码类 ViewController: UIViewController { @IBOutlet weak var myTextview: UITextView! var totalLines:CGFloat = 5 var maxHeight:CGFloat?覆盖 func viewDidLoad() { super.viewDidLoad() maxHeight = (myTextview.font!.lineHeight * totalLines) myTextview.sizeThatFits(CGSizeMake(myTextview.frame.size.width, maxHeight!)) } 覆盖 func didReceiveMemoryWarning() { super. didReceiveMemoryWarning() } }
标签: swift2