Name: Marklight
Owner: Wire Swiss GmbH
Description: Markdown syntax highlighter for iOS
Forked from: macteo/Marklight
Created: 2017-07-04 14:07:05.0
Updated: 2017-10-19 12:27:34.0
Pushed: 2017-08-08 08:45:39.0
Homepage: null
Size: 2509
Language: Swift
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Markdown syntax highlighter for iOS.
Marklight is a drop in component to easily add realtime markdown syntax highlight on any user editable text view on iOS applications. Marklight doesn't include html generation from markdown, but you can use one of the many other components available like Markingbird.
Regular expressions are taken from Markingbird: markdown parser and html generator.
UITextView
.NSTextStorage
subclass ready to use.CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
m install cocoapods
CocoaPods 1.0.0+ is required to build Marklight.
To integrate Marklight into your Xcode project using CocoaPods, specify it in your Podfile
:
ce 'https://github.com/CocoaPods/Specs.git'
form :ios, '8.0'
frameworks!
'Marklight'
Then, run the following command:
d install
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
ew update
ew install carthage
To integrate Marklight into your Xcode project using Carthage, specify it in your Cartfile
:
ub "macteo/Marklight"
Run carthage update --platform iOS
to build the framework and drag the built Marklight.framework
into your Xcode project.
Add the Marklight Xcode project to your own. Then add the Marklight
framework as desired to the embedded binaries of your app's target.
In this repository you can find a sample project with few lines of code in the ViewController
class for a jumpstart.
Sample code is written in Swift but Objective-C should be supported too, if you find an incompatibility please open an issue.
The easiest way to crete a user editable UITextView
with markdown syntax highlight is to use the provided MarklightTextStorage
class as NSTextStorage
and add the UITextView
's textLayout
to the MarklightTextStorage
text storage.
Import Marklight modules into your Swift class
rt Marklight
or if you are writing in Objective-C
ort <Marklight/Marklight-Swift.h>
Keep in mind the you have to let the project generate the Bridging Header otherwise the integration may fail.
In your UIViewController
subclass keep a strong instance of the this MarklightTextStorage
class.
textStorage = MarklightTextStorage()
Customize the appearance as desired:
As per Apple's documentation it should be enough to assign the UITextView
's NSLayoutManager
to the NSTextStorage
subclass, in our case MarklightTextStorage
.
tStorage.addLayoutManager(textView.layoutManager)
However I'm experiencing some crashes if I want to preload some text instead of letting the user start from scratch with a new text. A workaround is proposed below.
For simplicity we assume you have a String
to be highlighted inside an editable UITextView
loaded from a storyboard.
string = "# My awesome markdown string"
Convert string
to an NSAttributedString
attributedString = NSAttributedString(string: string)
Set the loaded string to the UITextView
View.attributedText = attributedString
Append the loaded string to the NSTextStorage
Storage.appendAttributedString(attributedString)
Enjoy.
Matteo Gavagnin ? @macteo
Marklight is heavily based on Markingbird, so many thanks to Kristopher Johnson and every previous contribution on which Markingbird is based upon.
Marklight is released under the MIT license. See LICENSE for details.