我一直在尝试使用 iOS 15 中发布的新AttributedString来渲染存储在变量中的 Markdown。但是,我一直无法找到一种方法来呈现 Markdown 标题,例如:
# Title 1
### Title 3
###### Title 6
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
# Title 1
### Title 3
###### Title 6
Run Code Online (Sandbox Code Playgroud)
但预览中显示的内容如下:
有没有人成功地让他们工作或者这是目前不可能做到的事情?
我正在尝试使用 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) 我正在努力替换NSAttributedString,AttributedString但未能成功使附件正常工作。尽管我应用了附件,但图像并未出现在字符串中。
let textAttachment = NSTextAttachment(image: UIImage(systemName: "exclamationmark.triangle.fill")!)
textAttachment.accessibilityLabel = "Warning"
// Original code
label.attributedText = NSAttributedString(attachment: textAttachment)
// New code
var attributedString = AttributedString()
attributedString.attachment = textAttachment
label.attributedText = NSAttributedString(attributedString)
Run Code Online (Sandbox Code Playgroud) 给定 markdown 字符串 "**Line 1**\n\nLine 2" 我期望输出为
1号线
2号线
相反我得到
1号线2号线
当然,这不是 markdown 或 AttributedString 的限制。我错过了什么?!如果没有两个空行,如何指定多个段落?
struct DemoView_Previews: PreviewProvider {
static var previews: some View {
Text(try! AttributedString(markdown: "**Line 1**\n\nLine 2"))
}
}
Run Code Online (Sandbox Code Playgroud) 从 iOS 15 开始,Swift 提供了 AttributedString 结构,它包含字符串的文本及其样式属性。问题:给定一个现有的 AttributedString,并假设(为了简单起见)属性由单个样式运行组成,如何仅更改AttributedString 的字符串部分?
\n这是一个典型的用例 \xe2\x80\x94 UIButton。假设我有一个基于配置的按钮,带有属性标题:
\nlet button = UIButton(configuration: .plain())\nlet font = UIFont(name: "Georgia", size: 16)\nbutton.configuration?.attributedTitle = AttributedString(\n "Hello", attributes: AttributeContainer.font(font!)\n)\nRun Code Online (Sandbox Code Playgroud)\n如果我稍后将配置的标题设置为不同的标题,则属性信息将丢失。例如:
\nbutton.configuration?.title = "Goodbye"\n// Button title is no longer in Georgia font!\nRun Code Online (Sandbox Code Playgroud)\n显然,我在这里想做的是替换属性字符串标题的文本而不干扰其属性。但 Swift 的 AttributedString 似乎没有提供一种方法来做到这一点。
\n因此,正如我一开始就问的:正确的方法是什么?
\nAttributedString我尝试使用SwiftUI 3 中的新功能
我想改变对齐方式,但它不起作用。其他任何事情都工作正常,例如我尝试使用属性字符串更改颜色,它确实有效!但我无能为力paragraphStyle
struct ContentView: View {
var body: some View {
ZStack {
Color(uiColor: UIColor(red: 0.92, green: 0.92, blue: 0.92, alpha: 1.00)).ignoresSafeArea()
VStack {
Text("""
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but …Run Code Online (Sandbox Code Playgroud)