【发布时间】:2020-07-11 10:52:05
【问题描述】:
如果是文字我会写
Text("Hello ")+Text("world").foregroundColor(.yellow)
我可以在TextField / TextEditor 中做什么?
【问题讨论】:
如果是文字我会写
Text("Hello ")+Text("world").foregroundColor(.yellow)
我可以在TextField / TextEditor 中做什么?
【问题讨论】:
我发现了一个可怕的 SwiftUI 解决方案:
@State private var text = "Hello world"
var body: some View {
VStack {
ZStack(alignment: .topLeading) {
TextEditor(text: $text)
.foregroundColor(.clear)
text
.split(separator: " ")
.map {
if $0 == "world" {
return Text($0 + " ").foregroundColor(.red)
} else {
return Text($0 + " ")
}
}
.reduce(Text(""), +)
.padding(.vertical, 8.5)
.padding(.horizontal, 5)
}
}.padding()
}
这种方法的问题:
【讨论】: