【发布时间】:2018-11-15 14:41:10
【问题描述】:
我正在尝试使用 swift 4 在 ios 中使用 google 的材料设计制作多行文本字段。
【问题讨论】:
标签: ios swift material-design
我正在尝试使用 swift 4 在 ios 中使用 google 的材料设计制作多行文本字段。
【问题讨论】:
标签: ios swift material-design
您可以使用google提供的MDCTextInputControllerOutlinedTextArea多行文本字段,您可以在google documentation中找到一些示例
如果您不想使用 google 组件,我建议您尝试使用 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
}
}
【讨论】: