如何使用swift 4在ios中使用Material Components实现多行文本字段



我正试图使用swift 4在ios中使用谷歌的材料设计来制作一个多行文本字段。

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

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

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

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

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
}
}

最新更新