17

Now that NSAttributedString is fully supported in iOS 6, is there a library that will take an NSString with markdown, and convert it to NSAttributedString?

Jason
  • 14,517
  • 25
  • 92
  • 153

7 Answers7

11

I've just added an NSString to NSAttributedString lightweight markup parser to MGBoxKit. It's not Markdown but it's very similar. So far it supports bold, italics, underline, monospacing, text colour, background colour, and kerning.

The MGMushParser class is now a standalone pod, so can easily be used independent of MGBoxKit.

NSString *markup = @"**bold**, //italics//, __underlining__, `monospacing`, and {#0000FF|text colour}";

UIFont *baseFont = [UIFont fontWithName:@"HelveticaNeue" size:18];
UIColor *textColor = UIColor.whiteColor;

myLabel.attributedString = [MGMushParser attributedStringFromMush:markup
                               font:baseFont color:textColor];
sobri
  • 1,626
  • 15
  • 28
6

I just open-sourced a project that takes raw markdown and converts it into an NSAttributedString:

https://github.com/dreamwieber/AttributedMarkdown

It's a work-in-progress and includes a demo application which shows how to assign attributes to the various markdown elements.

Dreamwieber
  • 422
  • 5
  • 10
1

DTCoreText converts html to NSAttributedString, also OHAttributedLabel have some basic markdown support.

Mark Pervovskiy
  • 1,123
  • 11
  • 18
  • 1
    Just a note if you need high performance do not use DTCoreText for HTML, it's still really inefficient for rendering/parsing. Use Bypass (ObjC) or Cocoa Markdown (swift) – Allison Feb 18 '18 at 16:36
1

For future reference, since this was the first post I found, you can now use NSAttributedString initWithData & NSDocumentTypeDocument to parse simple html on iOS 7

It seems easy enough to use a Markdown parser to HTML, then initWithData to do the rest. See http://initwithfunk.com/blog/2013/09/29/easy-markdown-rendering-with-nsattributedstring-on-ios-7.

For the HTML conversion, see my answer in ios7 font size change when create nsattributedstring from html

Community
  • 1
  • 1
danomatika
  • 101
  • 1
  • 4
1

You can also have a look at https://github.com/xing/XNGMarkdownParser, works fast and reliably

gskbyte
  • 467
  • 3
  • 13
1

It looks like Bypass is a good alternative which is also frequently updated (unlike most other repos mentioned). It even provides a custom UIView subclass which takes care of the rendering itself. It also uses Core Text directly instead of a UITextView, which should be faster.

ctietze
  • 2,805
  • 25
  • 46
1

I was struggling a lot to fine full-Markdown library that it's easy to use and finally I found CocoaMarkdown.

It's really great, supports most of the syntax and have easy to use API. The only thing that was missing was image support, so I forked it and add full-image support, along with cacheing and enabling to use images wrapped in URLs, hope it will solve the problem I had for someone else:

https://github.com/X8/CocoaMarkdown

Krodak
  • 1,493
  • 14
  • 21