开源软件名称(OpenSource Name):M2mobi/Marky-Mark开源软件地址(OpenSource Url):https://github.com/M2mobi/Marky-Mark开源编程语言(OpenSource Language):Swift 99.5%开源软件介绍(OpenSource Introduction):Marky MarkMarky Mark is a parser written in Swift that converts markdown into native views. The way it looks it highly customizable and the supported markdown syntax is easy to extend. ExampleTo run the example project, clone the repo, and run Requirements
InstallationCocoaPods 1.0.0+ is required to build MarkyMark To integrate MarkyMark into your Xcode project using CocoaPods, specify it in your pod "markymark" Alternatively, add MarkyMark to your project using Swift Package Manager using:
Simple usageView with default stylinglet markDownView = MarkDownTextView()
markDownView.text = "# Header\nParagraph" View with modified stylingMarkymark has many styling options, please check the examples in the styling section of this readme. A simple example: let markDownView = MarkDownTextView()
markDownView.styling.headingStyling.textColorsForLevels = [
.orange, //H1 (i.e. # Title)
.black, //H2, ... (i.e. ## Subtitle, ### Sub subtitle)
]
markDownView.styling.linkStyling.textColor = .blue
markDownView.styling.paragraphStyling.baseFont = .systemFont(ofSize: 14)
markDownView.text = "# Header\nParagraph" Supported tags in the Default FlavorNote: Different tags can be supported by either extending the ContentfulFlavor (default) or by implementing a class that comforms to
Customizing default styleDefault Styling instance var styling = DefaultStyling() Paragraphs (regular text)Markdown example: styling.paragraphStyling.baseFont = .systemFont(ofSize: 14)
styling.paragraphStyling.textColor = .black
styling.paragraphStyling.contentInsets = UIEdgeInsets(top:0, left: 0, bottom: 5, right: 0)
styling.paragraphStyling.lineHeight = 4
styling.paragraphStyling.isBold = false
styling.paragraphStyling.isItalic = false
styling.paragraphStyling.textAlignment = .left HeadingsMarkdown example: styling.headingStyling.fontsForLevels = [
UIFont.boldSystemFontOfSize(24), //H1
UIFont.systemFontOfSize(18), //H2
UIFont.systemFontOfSize(16) //H3, ... (last item will be next levels as well)
]
styling.headingStyling.colorsForLevels = [
.red, //H1
.black, //H2, ... (last item will be next levels as well)
]
// Margins
styling.headingStyling.contentInsetsForLevels = [
UIEdgeInsets(top: 5, left: 0, bottom: 15, right: 10), // H1
UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 10) //H2, ... (last item will be next levels as well)
]
styling.headingStyling.isBold = false
styling.headingStyling.isItalic = false
styling.headingStyling.isUnderlined = false
styling.headingStyling.textAlignment = .left linkStylingMarkdown Example styling.linkStyling.textColor = .black
styling.linkStyling.baseFont = nil // Default: nil. Setting baseFont to nil will inherit font from paragraphStyling
styling.linkStyling.isBold = false
styling.linkStyling.isItalic = false
styling.linkStyling.isUnderlined = true List stylingMarkdown Example:
// By default a font will be used with the bullet character `•`. Use the follow properties to configure it's size and color:
styling.listStyling.bulletFont = .systemFont(ofSize: 14)
styling.listStyling.bulletColor = .black
// Bullets can also be images for more complex styling. When setting images, bullet font and color won't be used anymore
// Array of images used as bullet for each level of nested list items
styling.listStyling.bulletImages = [
UIImage(named: "circle"),
UIImage(named: "emptyCircle"),
UIImage(named: "line"),
UIImage(named: "square")
]
// Size of the images
styling.listStyling.bulletViewSize = CGSize(width: 16, height: 16)
styling.listStyling.baseFont = .systemFont(ofSize: 14)
styling.listStyling.contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 10)
//Amount of space underneath each list item
styling.listStyling.bottomListItemSpacing = 5
// Number of pixels to indent for each nested list level
styling.listStyling.listIdentSpace = 15
styling.listStyling.textColor = .black Styling is also possible for:
Please check the Advanced usageAdvanced usage is only needed for very specific cases. Making subsets of styling, making different styling combinations, supporting different Markdown rules (syntax) or modifying certain views after that have been generated. Custom styling objectsstruct CustomMarkyMarkStyling: Styling {
var headerStyling = CustomHeaderStyling()
var paragraphStyling = ParagraphStyling()
var linkStyling = ListStyling()
var itemStylingRules: [ItemStyling] {
return [headerStyling, paragraphStyling, linkStyling]
}
} You can implement Each styling rule can be applied to a markDownItem by comforming to
This will let the mechanism know it should apply your styling to a HeaderMarkDownItem You can inject your new styling object by passing it to the constructor of the
Adding your own rulesAdding a new rule requires three new classes of based on the following protocol:
Add the rule to MarkyMark markyMark.addRule(MyCustomRule()) Or when using the MarkdownTextView: markdownTextView.add(rule: MyCustomRule()) Add the block builder to your layout converter converter.addLayoutBlockBuilder(MyCustomLayoutBlockBuilder()) Or when using the MarkdownTextView use either of these options (depending on the configuration view or attributedString): markdownTextView.addViewLayoutBlockBuilder(MyCustomLayoutBlockBuilder()) markdownTextView.addAttributedStringLayoutBlockBuilder(MyCustomLayoutBlockBuilder()) If needed you can also add a custom styling class to the default styling styling.addStyling(MyCustomStyling()) Converter hookThe converter has a callback method which is called every time a converter.didConvertElement = {
markDownItem, view in
// Do something with markDownItem and / or view here
} When using the MarkdownTextView markDownTextView.onDidConvertMarkDownItemToView = {
markDownItem, view in
} Link behaviorBy default Markymark opens URL's using let markDownView = MarkDownTextView()
markDownTextView?.urlOpener = MyCustomerURLOpener() Using Markymark in ExtensionsMarkymark also supports usage the a Today extension. By default tapping url's is not working, since Extensions don't have access to UIApplication.shared, in order to support links you can pass a different url opener to a MarkyDownTextView. See the Example project for a working example: markDownTextView?.urlOpener = ExtensionContextURLOpener(extensionContext: self.extensionContext) AuthorM2mobi, info@m2mobi.com LicenseMarkyMark is available under the MIT license. See the LICENSE file for more info. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论