Fri*_*ato 1 markdown ios swift swiftui
假设我想向用户显示以下文本Text:
3+4*5, 6*7+8
Run Code Online (Sandbox Code Playgroud)
iOS 15 中的新 SwiftUI 行为将在 Markdown 中呈现此字符串,使中心部分变为斜体。
请参阅此处:使用 Swift 进行黑客攻击
我可以这样做,并且它可以在 iOS 15 上运行:
Text("3+4\\*5, 6\\*7+8")
Run Code Online (Sandbox Code Playgroud)
但在 iOS 14 上,由于缺少 Markdown 解析,会显示如下:
3+4\*5, 6\*7+8
Run Code Online (Sandbox Code Playgroud)
有没有办法选择退出 Markdown 解析,以便我可以使用单个字符串定位 iOS 15 和 14?
根据您使用的初始化程序,将取决于它是否会渲染 markdown。您不需要使用转义字符,只需以正确的方式设置字符串即可。
以下详细介绍了如何在有或没有本地化的情况下拥有或不拥有 Markdown。
如果您使用默认值Text()并将字符串直接传递给它,那么它会假设您传递的是 a LocalizedStringKey,并且它将对其进行本地化并使用标记进行渲染。你可以在这里阅读更多。
如果您将字符串作为变量,那么 SwiftUI 将使用Text()您正在使用实际值的初始值设定项String,并且它将在没有本地化和降价的情况下渲染它。你可以在这里阅读更多。
如果您使用Text(verbatim:),那么它将不使用本地化,也不使用 Markdown。你可以在这里阅读更多。
如果您想使用本地化但不使用 Markdown,那么您需要使用创建一个 localizedStringString(localized:)并将其传递给Text(). 你可以在这里阅读更多。
struct ContentView: View {
let stringVariable = "This is *markdown*"
let localizedString = String(localized: "This is *markdown*")
var body: some View {
VStack(spacing: 20) {
// 1
// Localization with markdown
// https://developer.apple.com/documentation/swiftui/text/init(_:tablename:bundle:comment:)
Text("This is *markdown*")
// 2
// No localization no markdown
// https://developer.apple.com/documentation/swiftui/text/init(_:)-9d1g4
Text(stringVariable)
// 3
// No localization no markdown
// https://developer.apple.com/documentation/swiftui/text/init(verbatim:)
Text(verbatim: "This is *markdown*")
// 4
// This is a localized string but no markdown
// https://developer.apple.com/documentation/swift/string/3867985-init
Text(localizedString)
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面产生以下结果
| 归档时间: |
|
| 查看次数: |
1569 次 |
| 最近记录: |