Install MarkdownKit via Carthage
masterTo integrate MarkdownKit using Carthage, add the following line to your Cartfile:
github "ivanbruel/MarkdownKit"Then, run the following command to build the dependencies:
carthage update --use-xcframeworksrepository·master·Indexed 21 days ago
https://github.com/bmoliveira/markdownkitA customizable and extensible Markdown parser for iOS and macOS that converts Markdown text into NSAttributedString. It supports standard elements like emphasis, headers, quotes, lists, code, and links, and allows for custom elements via the MarkdownElement protocol.
To integrate MarkdownKit using Carthage, add the following line to your Cartfile:
github "ivanbruel/MarkdownKit"Then, run the following command to build the dependencies:
carthage update --use-xcframeworksTo add new Markdown elements, implement the MarkdownElement protocol (or its descendants like MarkdownLink) and provide a regular expression. You then pass these custom elements into the MarkdownParser initializer via the customElements parameter.
Example of creating a custom subreddit link parser:
import MarkdownKit
class MarkdownSubreddit: MarkdownLink {
private static let regex = "(^|\s|\W)(/?r/(\w+)/?)"
override var regex: String {
return MarkdownSubreddit.regex
}
override func match(match: NSTextCheckingResult,
attributedString: NSMutableAttributedString) {
let subredditName = attributedString.attributedSubstringFromRange(match.rangeAtIndex(3)).string
let linkURLString = "http://reddit.com/r/\(subredditName)"
formatText(attributedString, range: match.range, link: linkURLString)
addAttributes(attributedString, range: match.range, link: linkURLString)
}
}
// Usage
let markdownParser = MarkdownParser(customElements: [MarkdownSubreddit()])
let markdown = "**/r/iosprogramming** can be *markdown* as well!"
label.attributedText = markdownParser.parse(markdown)Add MarkdownKit as a dependency in your Package.swift file:
.package(url: "https://github.com/bmoliveira/MarkdownKit.git", from: "1.7.2")To integrate MarkdownKit into your Xcode project using CocoaPods, add the following line to your Podfile:
pod "MarkdownKit"Then, run pod install in your terminal.
You can customize the appearance of Markdown elements (like bold, italic, and headers) and control which elements are enabled.
Key customization properties include:
font: Sets the base font for the parser.enabledElements: A set used to enable or disable specific Markdown features (e.g., .disabledAutomaticLink).bold.color: Sets the color for bold text.italic.font: Sets the font for italic text.header.fontIncrease: An integer determining how much the font size increases for headers.let markdownParser = MarkdownParser(font: UIFont.systemFont(ofSize: 18))
markdownParser.enabledElements = .disabledAutomaticLink
markdownParser.bold.color = UIColor.red
markdownParser.italic.font = UIFont.italicSystemFont(ofSize: 300)
markdownParser.header.fontIncrease = 4To transform Markdown text into an NSAttributedString, create an instance of MarkdownParser and call the parse(_:) method. The resulting attributed string can be assigned directly to a UILabel or similar text component.
let markdownParser = MarkdownParser()
let markdown = "I support a *lot* of custom Markdown **Elements**, even `code`!"
label.attributedText = markdownParser.parse(markdown)MarkdownKit supports the following standard elements via Regular Expressions:
*italic* or _italics_, **bold** or __bold__, ~~strikethrough~~# H1 through ###### H6> Quote* List, - List, + List`code` or ```code```[Links](url)