降价已正确解析,问题似乎是标题的表示意图没有做任何事情。
您可以查看演示意图并将样式手动应用到标题。
你必须使用interpretedSyntax: .full
这意味着空白将被忽略,因此您可能还想在每个块之后添加一个新行。
extension AttributedString {
init(styledMarkdown markdownString: String) throws {
var output = try AttributedString(
markdown: markdownString,
options: .init(
allowsExtendedAttributes: true,
interpretedSyntax: .full,
failurePolicy: .returnPartiallyParsedIfPossible
),
baseURL: nil
)
for (intentBlock, intentRange) in output.runs[AttributeScopes.FoundationAttributes.PresentationIntentAttribute.self].reversed() {
guard let intentBlock = intentBlock else { continue }
for intent in intentBlock.components {
switch intent.kind {
case .header(level: let level):
switch level {
case 1:
output[intentRange].font = .system(.title).bold()
case 2:
output[intentRange].font = .system(.title2).bold()
case 3:
output[intentRange].font = .system(.title3).bold()
default:
break
}
default:
break
}
}
if intentRange.lowerBound != output.startIndex {
output.characters.insert(contentsOf: "\n", at: intentRange.lowerBound)
}
}
self = output
}
}
据我所知仅baselineOffset
, backgroundColor
, font
, foregroundColor
, kern
, strikethroughStyle
, tracking
, and underlineStyle
SwiftUI 支持。
这不是一个完美的解决方案,但它可能会让您更接近您所需要的。