我正在尝试在SwiftUI中设置文本编辑器的样式,使其看起来像 Material Design 元素。目前,我正在为浮动标签而苦苦挣扎。我不知道如何捕捉文本编辑器的编辑状态。这是应用程序的代码:
struct FloatingTextEditor: View {
let textFieldHeight: CGFloat = 168
private let placeHolderText: String
@Binding var text: String
@State private var isEditing = false
public init(placeHolder: String,
text: Binding<String>) {
self._text = text
self.placeHolderText = placeHolder
}
var shouldPlaceHolderMove: Bool {
isEditing || (text.count != 0)
}
var body: some View {
ZStack(alignment: .topLeading) {
TextEditor(text: $text)
.onTapGesture {
isEditing = true
}
.padding()
.frame(height: textFieldHeight)
.paragraph()
.overlay(RoundedRectangle(cornerRadius: 4)
.stroke(Color.medium, lineWidth: 1))
.animation(.linear)
Text(placeHolderText)
.foregroundColor(Color.medium)
.background(Color(UIColor.systemBackground))
.padding(shouldPlaceHolderMove ?
EdgeInsets(top: 0, leading:15, bottom: (textFieldHeight+30), trailing: 0) :
EdgeInsets(top: 0, leading:15, bottom: (textFieldHeight-10), trailing: 0))
.scaleEffect(shouldPlaceHolderMove ? 1.0 : 0.8)
.animation(.linear)
}
}}
基本上我需要帮助来弄清楚然后文本编辑器被敲出。
这是它的设计供参考: 文本编辑器