这是的自定义功能
Text
然而,你可以模仿一些行为。请参见以下示例:
import SwiftUI
struct MyText: View {
private let configurations: [Configuration]
init(_ title: String, foregroundColor: Color = .black) {
self.configurations = [
.init(title: title,
foregroundColor: foregroundColor)
]
}
private init(configurations: [Configuration]) {
self.configurations = configurations
}
private struct Configuration: Identifiable {
let id = UUID()
let title: String
let foregroundColor: Color
}
var body: some View {
HStack {
ForEach(configurations, content: Render.init)
}
}
static func + (lhs: Self, rhs: Self) -> Self {
let configurations = lhs.configurations + rhs.configurations
return MyText(configurations: configurations)
}
private struct Render: View {
let configuration: Configuration
var body: some View {
Text(configuration.title)
.foregroundColor(configuration.foregroundColor)
}
}
}
struct MyText_Previews: PreviewProvider {
static var previews: some View {
MyText("hej") + MyText("Yo", foregroundColor: .red)
}
}
然而,这更多的是一种概念证明,而不是一种实践建议。