如何使用swift iOS(不使用extview)进行多行扩展UTextField或文本输入

yftpprvb  于 2023-10-15  发布在  Swift
关注(0)|答案(2)|浏览(167)

看看苹果消息应用程序的行为,似乎他们正在使用一个文本输入字段。
光标出现在占位符文本开头的占位符的行为(本质上是占位符)是我假设的基础。据我所知,没有一个占位符可以用于extview。
我知道我可以很容易地使用一个extextView并添加一个不同颜色的占位符,并对占位符进行编码,当用户开始输入时,占位符就会消失。但是我想知道是否有任何方法可以编程一个可以让它的框架在宽度上固定并在高度上扩展 Package 文本的extField。

c3frrgcw

c3frrgcw1#

您不能扩展xmlextFiled的高度。
但是你可以使用ExtView来实现,你要做的是
使用extextView代替extFiled,并将ScrollEnabled设置为false,给予extView top、bottom、leading和trailing约束。
给予高度约束给extView,并将其内容优先级设置为低。
现在你的文本视图将根据插入的文本展开。
是的,您必须手动添加和管理占位符逻辑

nue99wik

nue99wik2#

下面是以编程方式创建多行扩展UTextField的完整工作代码:

  1. import UIKit
  2. class ExpandableTextFieldViewController: UIViewController, UITextViewDelegate {
  3. // Create a UITextView
  4. let textView: UITextView = {
  5. let textView = UITextView()
  6. textView.translatesAutoresizingMaskIntoConstraints = false
  7. textView.isScrollEnabled = false // Disable scrolling
  8. textView.font = UIFont.systemFont(ofSize: 16) // Set your preferred font size
  9. textView.layer.borderColor = UIColor.gray.cgColor
  10. textView.layer.borderWidth = 1.0
  11. textView.layer.cornerRadius = 5.0
  12. textView.textContainerInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8) // Adjust padding
  13. return textView
  14. }()
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. // Add the UITextView to the view
  18. view.addSubview(textView)
  19. // Set delegates
  20. textView.delegate = self
  21. // Add constraints
  22. NSLayoutConstraint.activate([
  23. textView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
  24. textView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
  25. textView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
  26. textView.bottomAnchor.constraint(lessThanOrEqualTo: view.bottomAnchor, constant: -20), // Ensure it doesn't go beyond the bottom of the screen
  27. // Set an initial height constraint (you can change this to your preferred initial height)
  28. textView.heightAnchor.constraint(equalToConstant: 100).withPriority(.defaultLow) // Set content hugging priority to low
  29. ])
  30. // Customize placeholder text and color
  31. textView.text = "Enter text here"
  32. textView.textColor = UIColor.lightGray
  33. }
  34. // MARK: - UITextViewDelegate methods
  35. func textViewDidChange(_ textView: UITextView) {
  36. // Adjust the height of the UITextView based on its content
  37. let fixedWidth = textView.frame.size.width
  38. let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: .greatestFiniteMagnitude))
  39. textView.heightAnchor.constraint(equalToConstant: newSize.height).isActive = true
  40. }
  41. func textViewDidBeginEditing(_ textView: UITextView) {
  42. // Remove placeholder text when the user starts editing
  43. if textView.textColor == UIColor.lightGray {
  44. textView.text = nil
  45. textView.textColor = UIColor.black
  46. }
  47. }
  48. func textViewDidEndEditing(_ textView: UITextView) {
  49. // Add placeholder text if the user ends editing with an empty field
  50. if textView.text.isEmpty {
  51. textView.text = "Enter text here"
  52. textView.textColor = UIColor.lightGray
  53. }
  54. }
  55. }
展开查看全部

相关问题