如何在 SwiftUI 的同一行中使用粗体和普通文本

How to use bold and normal text in same line in SwiftUI

我想在 SwiftUI 的一个 Text 视图中使用多种文本格式。这看起来像这样:

i want to have multiple formats for text in the same text box at the same time even if it is really really really really really long text so it would need to wrap correctly. (and maybe even include links!)

我在基本搜索后找到了 话题,但这根本不是我要找的。

这类似于 HTMLspan

这里有一个简单的示例,使用我的示例代码,您也可以为 iOS 13 或 14 编码:

struct ContentView: View {
    
    var body: some View {
        
        Text("Hello, World!") + Text(" Hello, World!").bold() + Text(" Hello, World!").italic()
        
    }
    
}

输出:

有了iOS 15,你可以为Text使用markdown:

Text("i want to have multiple **formats** for text in the _same **text box**_ at the same time even if it is really _really_ **really _really_** ~~really~~ long text so it would need to wrap correctly. (and maybe even include [links](https://www.apple.com)!)")

结果: