我正在尝试使用 SwiftUI 自定义更改给定 Markdown 字符串中超链接的默认字体颜色。相当于txtString.linkTextAttributes = [ .foregroundColor: UIColor.red ]
UIKit 的东西。这是我的代码:
import SwiftUI
struct TextViewAttributedString: View {
var markdownString: String
var body: some View {
Text(convertIntoAttributedString(markdownString:markdownString))
}
private func convertIntoAttributedString(markdownString: String) -> AttributedString {
guard var attributedString = try? AttributedString(
markdown: markdownString,
options: AttributedString.MarkdownParsingOptions(allowsExtendedAttributes: true,
interpretedSyntax: .inlineOnlyPreservingWhitespace))
else {
return AttributedString(markdownString)
}
attributedString.font = .custom("Times New Roman", size: 16, relativeTo: .body)
let runs = attributedString.runs
for run in runs {
let range = run.range
if let textStyle = …
Run Code Online (Sandbox Code Playgroud)