【问题标题】:how to make autoresize textfield, who adjust size with text input in swift如何制作自动调整大小的文本字段,谁在 swift 中使用文本输入调整大小
【发布时间】: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


【解决方案1】:

如果您明确提到了问题中提到的链接,那么答案建议您需要使用UITextView 而不是UITextField

嗯,你可以使用约束来增加UITextView 的高度,也可以使用这个库:

https://github.com/ingridwarrior2901/TextViewAutoHeight-Swift

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 2011-02-28
    • 1970-01-01
    • 2015-05-03
    • 2014-09-25
    • 1970-01-01
    • 2012-11-23
    相关资源
    最近更新 更多