在SwiftUI中,您可以毫不费力地将多个文本元素组合成+ operator倍的单个文本,并在该文本中合并图像.下面是如何在文本中放置图片的方法:
Text("You can also ")
+ Text(Image(systemName: "pencil.line"))
+ Text(" edit the text")
您可以灵活地将修饰符单独或整体应用于文本元素.
Collective Modifier Application:个
要整体应用修饰符,请将所有文本元素括在第一组括号中,然后应用修饰符,如下所示:
(Text("You can also ")
+ Text(Image(systemName: "pencil.line"))
+ Text(" edit the text"))
.font(.title2)
.fontWeight(.medium)
.foregroundStyle(.green)
Applying Modifiers Individually:个
您可以轻松地将修饰符应用于单个文本元素,就像SwiftUI中的任何其他视图一样.
Text("You can also ")
.font(.title2)
.fontWeight(.medium)
.foregroundStyle(.green)
+ Text(Image(systemName: "pencil.line"))
.fontWeight(.bold)
.foregroundStyle(.blue)
+ Text(" edit the text")
.italic()
.fontWeight(.bold)