SwiftMarkdown 是一个用于解析和格式化带有 markdown 标签的字符串的库
要使您的标签像上面那样格式化,您的代码将如下所示
let string = """
Swift **Markdown** Library
Swift ~~Markdown~~ Library
Swift *Markdown* Library
Swift ~~**Markdown**~~ Library
""".uppercased()
label.attributedText = string.markdown(font: label.font)
或者,如果您已经有一个 NSAttributedString
,只需将 markdown 标签添加到您的字符串,并使用 markdown
属性
let string = "SWIFT **MARKDOWN** LIBRARY"
let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20)]
let attributed = NSAttributedString(string: string, attributes: attributes)
label.attributedText = attributed.markdown
pod 'SwiftMarkdown'
将以下内容作为依赖项添加到您的 Package.swift
.package(url: "https://github.com/viniciusaro/SwiftMarkdown.git", .upToNextMajor(from: "0.0.2"))
您还需要在您希望使用 SwiftMarkdown 的 Target 中,将 “SwiftMarkdown” 指定为依赖项。
...
targets: [
.target(
name: "MyApp",
dependencies: ["SwiftResolver", "SwiftMarkdown"]),
...
*Italic*
**Bold**
~~Strikethrough~~
# H1
## H2
### H3
#### H4
##### H5
###### H6
SwiftMarkdown 在 MIT 许可证下发布。