CocoaMarkdown alternatives and similar libraries
Based on the "Text" category.
Alternatively, view CocoaMarkdown alternatives based on common mentions on social networks and blogs.
-
PhoneNumberKit
A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber. -
ZSSRichTextEditor
A beautiful rich text WYSIWYG editor for iOS with a syntax highlighted source view -
Twitter Text Obj
Twitter Text Libraries. This code is used at Twitter to tokenize and parse text to meet the expectations for what can be used on the platform. -
FontAwesomeKit
Icon font library for iOS. Currently supports Font-Awesome, Foundation icons, Zocial, and ionicons. -
TwitterTextEditor
A standalone, flexible API that provides a full-featured rich text editor for iOS applications. -
RichEditorView
DISCONTINUED. RichEditorView is a simple, modular, drop-in UIView subclass for Rich Text Editing. -
SwiftyMarkdown
Converts Markdown files and strings into NSAttributedStrings with lots of customisation options. -
Atributika
Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement. -
SwiftIconFont
Icons fonts for iOS (Font Awesome 5, Iconic, Ionicon, Octicon, Themify, MapIcon, MaterialIcon, Foundation 3, Elegant Icon, Captain Icon) -
NSStringEmojize
A category on NSString to convert Emoji Cheat Sheet codes to their equivalent Unicode characters -
Mustard
🌭 Mustard is a Swift library for tokenizing strings when splitting by whitespace doesn't cut it. -
Heimdall
Heimdall is a wrapper around the Security framework for simple encryption/decryption operations. -
AttributedTextView
Easiest way to create an attributed UITextView (with support for multiple links and from html)
SaaSHub - Software Alternatives and Reviews
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of CocoaMarkdown or a related project?
README
CocoaMarkdown
Markdown parsing and rendering for iOS and macOS
CocoaMarkdown is a cross-platform framework for parsing and rendering Markdown, built on top of the C reference implementation of CommonMark.
Why?
CocoaMarkdown aims to solve two primary problems better than existing libraries:
- More flexibility. CocoaMarkdown allows you to define custom parsing hooks or even traverse the Markdown AST using the low-level API.
- Efficient
NSAttributedString
creation for easy rendering on iOS and macOS. Most existing libraries just generate HTML from the Markdown, which is not a convenient representation to work with in native apps.
[Example app macOS](images/example-app-mac.png)
[Example app iOS](images/example-app-iOS.png)
Installation
First you will want to add this project as a submodule to your project:
git submodule add https://github.com/indragiek/CocoaMarkdown.git
Then, you need to pull down all of its dependencies.
cd CocoaMarkdown
git submodule update --init --recursive
Next, drag the .xcodeproj
file from within CocoaMarkdown
into your project. After that, click on the General tab of your target. Select the plus button under "Embedded Binaries" and select the CocoaMarkdown.framework.
API
Traversing the Markdown AST
[CMNode
](CocoaMarkdown/CMNode.h) and [CMIterator
](CocoaMarkdown/CMIterator.h) wrap CommonMark's C types with an object-oriented interface for traversal of the Markdown AST.
let document = CMDocument(contentsOfFile: path, options: [])
document.rootNode.iterator().enumerateUsingBlock { (node, _, _) in
print("String value: \(node.stringValue)")
}
Building Custom Renderers
The [CMParser
](CocoaMarkdown/CMParser.h) class isn't really a parser (it just traverses the AST), but it defines an NSXMLParser
-style delegate API that provides handy callbacks for building your own renderers:
@protocol CMParserDelegate <NSObject>
@optional
- (void)parserDidStartDocument:(CMParser *)parser;
- (void)parserDidEndDocument:(CMParser *)parser;
...
- (void)parser:(CMParser *)parser foundText:(NSString *)text;
- (void)parserFoundHRule:(CMParser *)parser;
...
@end
[CMAttributedStringRenderer
](CocoaMarkdown/CMAttributedStringRenderer.h) is an example of a custom renderer that is built using this API.
Rendering Attributed Strings
[CMAttributedStringRenderer
](CocoaMarkdown/CMAttributedStringRenderer.h) is the high level API that will be useful to most apps. It creates an NSAttributedString
directly from Markdown, skipping the step of converting it to HTML altogether.
Going from a Markdown document to rendering it on screen is as easy as:
let document = CMDocument(contentsOfFile: path, options: [])
let renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes())
textView.attributedText = renderer.render()
Or, using the convenience method on CMDocument
:
textView.attributedText = CMDocument(contentsOfFile: path, options: []).attributedStringWithAttributes(CMTextAttributes())
HTML elements can be supported by implementing [CMHTMLElementTransformer
](CocoaMarkdown/CMHTMLElementTransformer.h). The framework includes several transformers for commonly used tags:
- [
CMHTMLStrikethroughTransformer
](CocoaMarkdown/CMHTMLStrikethroughTransformer.h) - [
CMHTMLSuperscriptTransformer
](CocoaMarkdown/CMHTMLSuperscriptTransformer.h) - [
CMHTMLSubscriptTransformer
](CocoaMarkdown/CMHTMLSubscriptTransformer.h)
Transformers can be registered with the renderer to use them:
let document = CMDocument(contentsOfFile: path, options: [])
let renderer = CMAttributedStringRenderer(document: document, attributes: CMTextAttributes())
renderer.registerHTMLElementTransformer(CMHTMLStrikethroughTransformer())
renderer.registerHTMLElementTransformer(CMHTMLSuperscriptTransformer())
textView.attributedText = renderer.render()
Customizing Attributed strings rendering
All attributes used to style the text are customizable using the [CMTextAttributes
](CocoaMarkdown/CMTextAttributes.h) class.
Every Markdown element type can be customized using the corresponding CMStyleAttributes
property in CMTextAttributes
, defining 3 different kinds of attributes:
- String attributes, i.e. regular NSAttributedString attributes
- Font attributes, for easy font setting
- Paragraph attributes, relevant only for block elements
Attributes for any Markdown element kind can be directly set:
let textAttributes = CMTextAttributes()
textAttributes.linkAttributes.stringAttributes[NSAttributedString.Key.backgroundColor] = UIColor.yellow
A probably better alternative for style customization is to use grouped attributes setting methods available in CMTextAttributes
:
let textAttributes = CMTextAttributes()
// Set the text color for all headers
textAttributes.addStringAttributes([ .foregroundColor: UIColor(red: 0.0, green: 0.446, blue: 0.657, alpha: 1.0)],
forElementWithKinds: .anyHeader)
// Set a specific font + font-traits for all headers
let boldItalicTrait: UIFontDescriptor.SymbolicTraits = [.traitBold, .traitItalic]
textAttributes.addFontAttributes([ .family: "Avenir Next" ,
.traits: [ UIFontDescriptor.TraitKey.symbolic: boldItalicTrait.rawValue]],
forElementWithKinds: .anyHeader)
// Set specific font traits for header1 and header2
textAttributes.setFontTraits([.weight: UIFont.Weight.heavy],
forElementWithKinds: [.header1, .header2])
// Center block-quote paragraphs
textAttributes.addParagraphStyleAttributes([ .alignment: NSTextAlignment.center.rawValue],
forElementWithKinds: .blockQuote)
// Set a background color for code elements
textAttributes.addStringAttributes([ .backgroundColor: UIColor(white: 0.9, alpha: 0.5)],
forElementWithKinds: [.inlineCode, .codeBlock])
Font and paragraph attributes are incremental, meaning that they allow to modify only specific aspects of the default rendering styles.
Additionally on iOS, Markdown elements styled using the font attributes API get automatic Dynamic-Type compliance in the generated attributed string, just like default rendering styles.
Rendering HTML
[CMHTMLRenderer
](CocoaMarkdown/CMHTMLRenderer.h) provides the ability to render HTML from Markdown:
let document = CMDocument(contentsOfFile: path, options: [])
let renderer = CMHTMLRenderer(document: document)
let HTML = renderer.render()
Or, using the convenience method on CMDocument
:
let HTML = CMDocument(contentsOfFile: path).HTMLString()
Example Apps
The project includes example apps for iOS and macOS to demonstrate rendering attributed strings.
Contact
- Indragie Karunaratne
- @indragie
- http://indragie.com
License
CocoaMarkdown is licensed under the MIT License. See LICENSE
for more information.
*Note that all licence references and agreements mentioned in the CocoaMarkdown README section above
are relevant to that project's source code only.