• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

dreamwieber/AttributedMarkdown: Native Markdown Parsing to NSAttributedString on ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

dreamwieber/AttributedMarkdown

开源软件地址(OpenSource Url):

https://github.com/dreamwieber/AttributedMarkdown

开源编程语言(OpenSource Language):

Objective-C 85.9%

开源软件介绍(OpenSource Introduction):

AttributedMarkdown: Native Markdown Parsing on iOS

Markdown is intended to be as easy-to-read and easy-to-write as is feasible.

-- Daring Fireball

This library takes Markdown formatted text and turns it into an NSAttributedString, suitable for rendering in native UIKit components on iOS 6 (UITextView, UILabel, etc).

In short, this allows you to apply styling to Markdown without having to use UIWebView and HTML tags.

This project is based-upon / modifies a Cocoa fork of peg markdown.

Usage:

// start with a raw markdown string
NSString *rawText = @"Hello, world. *This* is native Markdown.";

// create a font attribute for emphasized text
UIFont *emFont = [UIFont fontWithName:@"AvenirNext-MediumItalic" size:15.0];

// create a color attribute for paragraph text
UIColor *color = [UIColor purpleColor];

// create a dictionary to hold your custom attributes for any Markdown types
NSDictionary *attributes = @{
  @(EMPH): @{NSFontAttributeName : emFont,},
  @(PARA): @{NSForegroundColorAttributeName : color,}
};

// parse the markdown
NSAttributedString *prettyText = markdown_to_attr_string(rawText,0,attributes);

// assign it to a view object
myTextView.attributedText = prettyText;

Check out the HelloMarkdown example app to see it in action.

Easy Setup (BETA)

A Cocoapod Podspec file has been added that will allow you to do a pod install to get up and running quickly. Eventually we'll post it to the cocoapods repo. For now, here's what your podfile should look like:

platform :ios
pod 'AttributedMarkdown', :git => 'https://github.com/dreamwieber/AttributedMarkdown.git'

(The HelloMarkdown project hasn't been updated or tested against a cocoapod install. If you find any issues please let us know. This should work fine for your new project though.)

Requirements & Setup

If you don't want to do Cocoapods, you can build the library yourself. There are some dependencies, which have proven tricky for some. Unless you need/want to modify the parser, it's probably easier to go with Cocoapod install.

AttributedMarkdown makes use of a parser-generator called greg. This is included as a submodule, and you'll need to first run this from the command-line (from your project's root directory):

git submodule update --init --recursive

You'll also need to include the CoreText Framework in your project.

To use AttributedMarkdown in one of your projects, follow the standard Apple guidelines for linking against a static library

Finally, create a group in your project called "Headers" and copy these files into it:

markdown_lib.h
markdown_peg.h 

Leave the option to copy the files into your project unselected.

(Note that you don't have to call the group "Headers", this is simply a suggestion. The important bit is making sure the header references exist somewhere in your project.)

The import statements in your project, wherever you want to make use of the library (eg., in a View Controller) should look like:

#import "markdown_lib.h"
#import "markdown_peg.h"

Basic Cascading Styles

AttributedMarkdown performs some very basic cascading styles, merging the string attributes of parent elements into child elements by extracting their font traits via CoreText. This allows for things like emphasized words within an h1 tag to be bold as well as italicized.

Performance

Although I have yet to perform any optimizations, parsing and display of markdown in a UITableView filled with many cells of long-form markdown sample text performs rather well.

Limitations

This is a work in progress. Some tags are not yet supported, like img, etc.

Credit

AttributedMarkdown was created by Gregory Wieber and Jim Radford. It is based upon peg-markdown.

License

AttributedMarkdown is released under both the GPL and the MIT license; see LICENSE for details.

Peg-Markdown License

peg-markdown is written and maintained by John MacFarlane (jgm on github), with significant contributions by Ryan Tomayko (rtomayko). It is released under both the GPL and the MIT license; see LICENSE for details. peg-markdown was adapted for Cocoa by David Whetstone.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap