【问题标题】:How to implement multi-line text field using Material Components in ios using swift 4如何使用swift 4在ios中使用Material Components实现多行文本字段
【发布时间】:2018-11-15 14:41:10
【问题描述】:

我正在尝试使用 swift 4 在 ios 中使用 google 的材料设计制作多行文本字段。

【问题讨论】:

    标签: ios swift material-design


    【解决方案1】:

    您可以使用google提供的MDCTextInputControllerOutlinedTextArea多行文本字段,您可以在google documentation中找到一些示例

    如果您不想使用 google 组件,我建议您尝试使用 TextView 而不是 TextField

    您可以使用 TextView 提供的 sizeThatFits 函数轻松调整 TextView 的大小。

    【讨论】:

      【解决方案2】:

      如果您想以编程方式初始化多行文本字段:

      import UIKit
      import MaterialComponents.MDCMultilineTextField
      
      class MyMultilineTextField: MDCMultilineTextField {
      
          private var controller: MDCTextInputControllerOutlinedTextArea?
          private var placeholderText: String
      
          init(placeholder: String) {
              self.placeholderText = placeholder
      
              super.init(frame: .zero)
              initialize()
          }
      
          required init?(coder aDecoder: NSCoder) {
              fatalError("init(coder:) has not been implemented")
          }
      
          private func initialize() {
              translatesAutoresizingMaskIntoConstraints = false
              clearButtonMode = .whileEditing
      
              controller = MDCTextInputControllerOutlinedTextArea(textInput: self)
              controller?.placeholderText = placeholderText
          }
      }
      

      然后在您想要使用文本字段的项目中的其他地方:

      class SomeViewController: UIViewController {
      
          override func viewDidLoad() {
               super.viewDidLoad()
      
               let myTextField = MyMultilineTextField(placeholder: "Some text...")
      
               view.addSubView(myTextField)
      
               // Configure constraints for myTextField as necessary
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-29
        • 1970-01-01
        • 2018-09-25
        • 2020-12-29
        • 2020-09-14
        • 2017-12-21
        相关资源
        最近更新 更多