Parma
使用纯 SwiftUI 组件显示 Markdown。利用 ViewBuilder
为 Text
和 View
创建自定义外观。
示例
import Parma
struct ContentView: View {
var markdown = "I'm **Strong**."
var body: some View {
Parma(markdown)
}
}
更多示例,请参阅 demo 应用。
Markdown 支持
已支持
- 标题级别 1-6
- 段落
- 多级项目符号列表
- 图像(需要额外配置)
- 内联文本
- 粗体
- 斜体
- 代码
可能在未来的版本中得到支持
- 分隔符
- 多级有序列表
- 块引用
- 代码块
不支持的
- 行内超链接
安装
需求
- Xcode 11.0 或更高版本
- Swift 5 或更高版本
- iOS 13.0 / macOS 10.15 或更高版本的部署目标
Swift Package Manager
Swift Package Manager 是一个用于管理 Swift 代码分发的工具。它与 Swift 构建系统集成,以自动化所有平台上的依赖项的下载、编译和链接过程。
通过 使用 Xcode 的 GUI 将 Parma
添加为依赖项,包的 URL 为 https://github.com/dasautoooo/Parma
。
外观自定义
要自定义 Text
样式和 View
,创建一个新的符合 ParmaRenderable
协议的渲染器,并只实现那些适合你的目的的功能。最后,在创建 Parma
视图时分配自定义的渲染器。
import Parma
struct ContentView: View {
var markdown = "I'm **Strong**."
var body: some View {
Parma(markdown, render: MyRender())
}
}
struct MyRender: ParmaRenderable {
...
}
有一个 DemoApp 对以下一些代理方法进行了修改,供大家参考。
/// Define the heading text style.
/// - Parameters:
/// - level: The level of heading.
/// - textView: The textView generated from captured heading string.
func heading(level: HeadingLevel?, textView: Text) -> Text
/// Define the paragraph text style.
/// - Parameter text: The text string captured from paragraph.
func paragraph(text: String) -> Text
/// Define the text style for plain text. Do NOT recommend to alter this if there's no special purpose.
/// - Parameter text: The text string captured from markdown.
func plainText(_ text: String) -> Text
/// Define the strong text style.
/// - Parameter textView: The textView generated from captured strong string.
func strong(textView: Text) -> Text
/// Define the emphasis text style.
/// - Parameter textView: The textView generated from captured emphasis string.
func emphasis(textView: Text) -> Text
/// Define the link text style.
/// - Parameters:
/// - textView: The textView generated from captured link string.
/// - destination: The destination of the link.
func link(textView: Text, destination: String?) -> Text
/// Define the code text style.
/// - Parameter text: The text string captured from code.
func code(_ text: String) -> Text
/// Define the style of heading view.
/// - Parameters:
/// - level: The level of heading.
/// - view: The view contains heading text.
func headingBlock(level: HeadingLevel?, view: AnyView) -> AnyView
/// Define the style of paragraph view.
/// - Parameter view: The view contains view(s) which belong(s) to this paragraph.
func paragraphBlock(view: AnyView) -> AnyView
/// Define the style of list item.
/// - Parameter view: The view contains view(s) which belong(s) to this item.
func listItem(view: AnyView) -> AnyView
/// Define the style of image view.
/// - Parameter urlString: The url string for this image view.
/// - Parameter altTextView: The view contains alt text.
func imageView(with urlString: String, altTextView: AnyView?) -> AnyView
名称起源
帕尔马是意大利北部的一个城市,以建筑、音乐和艺术而著名。选择这个城市名称作为项目名称的原因是为了纪念吉安巴蒂斯塔·博多尼,一位著名的排版设计师,他大部分时间都生活在该城市并从事相关工作。
博多尼是一位意大利排版设计师,在帕尔马进行字体设计。在他的一生中,他设计了现在被广泛称为博多尼体的许多字体。每一台Mac都安装了博多尼字体,并且可以免费使用。
鸣谢
该程序基于Down构建,这是一个Swift的Markdown解析器。