Markdown to NSAttributedString library? [closed]

2019-02-04 14: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?

回答1:

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];


回答2:

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.



回答3:

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



回答4:

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



回答5:

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



回答6:

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.



回答7:

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