2016-03-22 3 views
1

Bir test projesi ile Markdown desteğini daha iyi anlamaya çalışıyorum. Bir kılavuz için MardownTextView projesini kullanıyorum - burada https://github.com/indragiek/MarkdownTextView bulundu.'textStorage' bir özelliktir - UITextView için MarkdownKit Framework'ü kullanırken hata

Test projem için MarkdownKit çerçevesini ekledim.

benim textViewExample proje ve MarkdownTextView projesi arasındaki fark

Ben storyboard içinde UITextView ekliyorum ve MarkdownTextView programlama yoluyla ekler olmasıdır.

MarkdownTextView projesi, çerçeveyi kullanarak UITextView ve işaretleme desteğini nasıl ekler.

import UIKit 
import MarkdownTextView 

class ViewController: UIViewController { 
override func viewDidLoad() { 
    super.viewDidLoad() 

    let attributes = MarkdownAttributes() 
    let textStorage = MarkdownTextStorage(attributes: attributes) 
    do { 
     textStorage.addHighlighter(try LinkHighlighter()) 
    } catch let error { 
     fatalError("Error initializing LinkHighlighter: \(error)") 
    } 
    textStorage.addHighlighter(MarkdownStrikethroughHighlighter()) 
    textStorage.addHighlighter(MarkdownSuperscriptHighlighter()) 
    if let codeBlockAttributes = attributes.codeBlockAttributes { 
     textStorage.addHighlighter(MarkdownFencedCodeHighlighter(attributes: codeBlockAttributes)) 
    } 

    let textView = MarkdownTextView(frame: CGRectZero, textStorage: textStorage) 
    textView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(textView) 

    let views = ["textView": textView] 
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[textView]-20-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) 
    constraints += NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[textView]-20-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views) 
    NSLayoutConstraint.activateConstraints(constraints) 
} 
} 

Testi Projesi

Zaten storyboard ve kısıtlamaları içinde görünüm kontrolöre benim UITextView ekledik. Bu yüzden aşağıdakileri yaparak destek eklemeyi denedim;

import UIKit 
import MarkdownKit 

@IBOutlet weak var textView: MarkdownTextView! 
var textStorage: MarkdownTextStorage? 

class TextViewController: UIViewController, UITextViewDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let attributes = MarkdownAttributes() 
    let textStorage = MarkdownTextStorage(attributes: attributes) 
    do { 
     textStorage.addHighlighter(try LinkHighlighter()) 
    } catch let error { 
     fatalError("Error initializing LinkHighlighter: \(error)") 
    } 
    textStorage.addHighlighter(MarkdownStrikethroughHighlighter()) 
    textStorage.addHighlighter(MarkdownSuperscriptHighlighter()) 
    if let codeBlockAttributes = attributes.codeBlockAttributes { 
     textStorage.addHighlighter(MarkdownFencedCodeHighlighter(attributes: codeBlockAttributes)) 
    } 

    //Error - Apply textStorage to textview that has already been created 
    textView.textStorage = textStorage 

} 
} 

ancak hatayı olsun

özelliğine atanamıyor

: 'TextStorage' bir olsun tek özellik I textView için markdown desteği eklemek nasıl

benim Örnek proje olarak çerçeveyi kullanan proje, programlı olarak UITextView eklemeye gerek kalmadan yapıldı mı?

cevap

1

TextView'un en layoutManager işlemek için kullanın onun TextStorage:

if let lm = self.textView.layoutManager{ 
    lm.replaceTextStorage(textStorage) 
} 
Sen NSLayoutManager class

Apple'ın belgelerinde bu konuda daha fazla bilgi bulabilirsiniz