MarkdownToAttributedString

一个 Swift 包,可以将 Markdown 内容转换为 NSAttributedString,适用于应用到 Apple UI 元素,例如 UILabelUITextViewNSTextFieldNSTextView 等。MarkdownToAttributedString 构建于 Apple 自己的 Swift Markdown 解析器之上,以实现最佳的性能、一致性和兼容性。虽然您可以使用 Apple 最新的 AttributedString 选项轻松完成此操作,但这些选项仅在 iOS 15+ 和 macOS 12+ 中可用,但我们很多人都在开发面向旧版本操作系统的应用程序。

兼容性

MarkdownToAttributedString 适用于所有版本的 iOS、macOS(嗯,Mac OS X+,哈)和 watchOS。

支持的 Markdown 类型

用法

基本示例

    import MarkdownToAttributedString

    @IBOutlet weak var someLabel: UILabel!

    let md = """
Some **bold text**, some *emphasized text*, some `inline code`.

Some ~~strikethrough text with **embedded** *style*~~.

\```
// A block of code
static let textColor = CocoaColor.black
static let cellPadding: CGFloat = 9
\```

* This is a list
    * **Item 1.1 (bold)**
    * Item 1.2
        * Item 1.2.1
* Item 2

**`some code` within a bold span**
*`some code` within an italics span*

1. An ordered list
1. with
    1. two
    1. sub-items

# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6

This [is my website ](https://madebywindmill.com).

"""

    someLabel.attributedText = AttributedStringFormatter.format(markdown: md)
    

带有自定义样式的示例

    import MarkdownToAttributedString

    @IBOutlet weak var someLabel: UILabel!

    let md = ... // as above

    let indentedPStyle = NSMutableParagraphStyle()
    indentedPStyle.firstLineHeadIndent = 20
    indentedPStyle.headIndent = 20
    
    let baseAttrs: StringAttrs = [.font: UIFont.systemFont(ofSize: 15),
                                  .foregroundColor: UIColor(hex3: 0x222222)]
    let styleAttrs: [MarkupType: StringAttrs] = [
        .strong: [
            .font: UIFont.boldSystemFont(ofSize: 15),
            .foregroundColor: UIColor.orange
        ],
        .emphasis: [
            .font: UIFont.italicSystemFont(ofSize: 15),
            .foregroundColor: UIColor.orange
        ],
        .inlineCode: [
            .font: UIFont.monospacedSystemFont(ofSize: 15, weight: .light),
            .foregroundColor: UIColor.black,
            .backgroundColor: UIColor(hex3: 0xEEEEEE)
        ],
        .codeBlock: [
            .font: UIFont.monospacedSystemFont(ofSize: 13, weight: .light),
            .foregroundColor: UIColor.black,
            .paragraphStyle: indentedPStyle
        ],
        .listItem: [
            .font: UIFont.systemFont(ofSize: 15, weight: .regular),
            .foregroundColor: UIColor.lightGray
        ],
        .heading: [
            .font: UIFont.boldSystemFont(ofSize: 15),
            .paragraphStyle: indentedPStyle
        ],
        .unorderedList: [
            .font: UIFont.monospacedSystemFont(ofSize: 15, weight: .regular),
            .paragraphStyle: indentedPStyle
        ],
        .orderedList: [
            .font: UIFont.monospacedSystemFont(ofSize: 15, weight: .regular),
            .paragraphStyle: indentedPStyle
        ],
        .link: [
            .font: CocoaFont.systemFont(ofSize: 15, weight: .regular),
        ]
    ]
    
    let markdownStyles = MarkdownStyles(baseAttributes: baseAttrs, styleAttributes: styleAttrs)
    let formatter = AttributedStringFormatter(markdown: md, styles: markdownStyles)
    let attrStr = formatter.format()
    someLabel.attributedText = attrStr

结果

Example result

MIT 许可证

版权所有 (c) 2025 Made by Windmill, LLC

特此授予任何人免费获得本软件及相关文档文件(“软件”)副本的许可,以不受限制地处理本软件,包括但不限于使用、复制、修改、合并、出版、分发、再许可和/或销售本软件的副本,并允许向获得本软件的人提供本软件,但须符合以下条件:

上述版权声明和本许可声明应包含在本软件的所有副本或主要部分中。

本软件按“原样”提供,不提供任何形式的明示或暗示保证,包括但不限于适销性、特定用途的适用性和不侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任承担责任,无论是在合同、侵权或其他行为中,因本软件或本软件的使用或其他处理而产生、由此产生或与之相关的责任。