我是SwiftUI的新手。
我创建了一个带有文本的数据和一个具有字符串定义的模型文件。数据文件包含一些单行和多行文本。
descriptionTop: "Beschreibung Oben:"和
descriptionTop: """
Der Vorstand kann Mitglieder, and some more lines of text ...
"""我想用粗体、斜体、划线等来格式化一些单词。我已经试着添加类似在另一篇文章中所描述的内容。
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)!)")但没有成功。我使用Xcode13项目和IOS15.5部署,谢谢您的支持。
发布于 2022-09-23 08:08:10
对于iOS 15+,您只需使用AttributedStrings即可。如果文本视图中有一个静态文本,您可以这样使用它:
Text("~~A strikethrough example~~")如果您从变量或请求中获得字符串,则此操作将无法工作。要解决这个问题,可以使用此方法将字符串转换为AttributedString:
func getAttributedString(_ markdown: String) -> AttributedString {
do {
let attributedString = try AttributedString(markdown: markdown, options: AttributedString.MarkdownParsingOptions(interpretedSyntax: .inlineOnlyPreservingWhitespace))
return attributedString
} catch {
print("Couldn't parse: \(error)")
}
return AttributedString("Error parsing markdown")
}这将转换字符串(例如,从变量中),并将其转换为可以在文本视图中使用的AttributedString,如下所示:
var example = "~~A strikethrough example~~"
Text(getAttributedString(example))https://stackoverflow.com/questions/73817340
复制相似问题