ViewController.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // ViewController.swift
  3. // Example-iOS
  4. //
  5. // Created by Indragie on 1/15/15.
  6. // Copyright (c) 2015 Indragie Karunaratne. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaMarkdown
  10. class ViewController: UIViewController {
  11. @IBOutlet var textView: UITextView!
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. let path = Bundle.main.path(forResource: "test", ofType: "md")!
  15. let document = CMDocument(contentsOfFile: path, options: CMDocumentOptions(rawValue: 0))
  16. let textAttributes = CMTextAttributes()
  17. // Customize the color and font of header elements
  18. textAttributes.addStringAttributes([ .foregroundColor: UIColor(red: 0.0, green: 0.446, blue: 0.657, alpha: 1.0)], forElementWithKinds: .anyHeader)
  19. let boldItalicTrait: UIFontDescriptor.SymbolicTraits = [.traitBold, .traitItalic]
  20. textAttributes.addFontAttributes([ .family: "Avenir Next" ,
  21. .traits: [ UIFontDescriptor.TraitKey.symbolic: boldItalicTrait.rawValue]],
  22. forElementWithKinds: .anyHeader)
  23. textAttributes.setFontTraits([.weight: UIFont.Weight.heavy], forElementWithKinds: [.header1, .header2])
  24. textAttributes.addFontAttributes([ .size: 40 ], forElementWithKinds: .header1)
  25. // Customize the font and paragraph alignment of block-quotes
  26. textAttributes.addFontAttributes([.family: "Noteworthy", .size: 18], forElementWithKinds: .blockQuote)
  27. textAttributes.addParagraphStyleAttributes([ .alignment: NSTextAlignment.center.rawValue], forElementWithKinds: .blockQuote)
  28. // Customize the background color of code elements
  29. textAttributes.addStringAttributes([ .backgroundColor: UIColor(white: 0.9, alpha: 0.5)], forElementWithKinds: [.inlineCode, .codeBlock])
  30. let renderer = CMAttributedStringRenderer(document: document, attributes: textAttributes)!
  31. renderer.register(CMHTMLStrikethroughTransformer())
  32. renderer.register(CMHTMLSuperscriptTransformer())
  33. renderer.register(CMHTMLUnderlineTransformer())
  34. textView.attributedText = renderer.render()
  35. }
  36. }