2016-02-09 46 views
7

私の目標は、Goole Docsテキストエディタに似たビューを作成し、コメントのあるテキストの後ろにハイライト表示することです。 HighlightNSViewとNSScrollView内のNSTextView

私のソリューションは、スクロールやテキストのNSTextViewの両方が含まれ、そして他のNSView sのハイライトになるということNSScrollViewが(ドキュメントビューとして設定)NSViewを含むことです。

これが機能するには、NSTextViewはあたかもNSScrollViewに属しているかのようにサイズを変更する必要があります。しかし、私はNSTextViewにこの動作をさせることはできません。

Iレイアウトを持っているコードは:

LatinViewController:loadViewメソッド()

... 
let latinView = LatinView() 
latinView.autoresizingMask = [.ViewWidthSizable] 
latinView.wantsLayer = true 
latinView.layer?.backgroundColor = Theme.greenColor().CGColor 
self.latinView = latinView 
scrollView.documentView = latinView 
... 

LatinView:INIT()

... 
let textView = LatinTextView() 
textView.translatesAutoresizingMaskIntoConstraints = false 
textView.string = "Long string..." 
self.addSubview(textView) 
self.textView = textView 

self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[text]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["text": textView])) 
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[text]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["text": textView])) 
... 

LatinTextViewます。init()

... 
self.minSize = NSMakeSize(0, 0) 
self.maxSize = NSMakeSize(0, CGFloat(FLT_MAX)) 
self.verticallyResizable = true 
self.horizontallyResizable = false 
self.textContainer?.heightTracksTextView = false 
... 

はこれを行うことができますか?

答えて

1

あなたの要件から、NSAttributedStringとNSTextViewを使用するだけで機能が実現するようです。サンプルコード A NSTextViewれる下記既に有している(一般にNSTextViewによって使用され、そしてNSAttributedString)NSAttributedString

import Cocoa 

@NSApplicationMain 
class AppDelegate: NSObject, NSApplicationDelegate { 

    @IBOutlet weak var window: NSWindow! 
    @IBOutlet var textView:NSTextView! 


    func applicationDidFinishLaunching(aNotification: NSNotification) { 
     let singleAttribute1 = [ NSForegroundColorAttributeName: NSColor.purpleColor() , NSBackgroundColorAttributeName: NSColor.yellowColor() ] 

     let multipleAttributes = [ 
      NSForegroundColorAttributeName: NSColor.redColor(), 
      NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue ] 

     var string1 = NSAttributedString(string: "Hello World", attributes: singleAttribute1) 

     var string2 = NSAttributedString(string: " This is knowstack", attributes: multipleAttributes) 

     var finalString = NSMutableAttributedString(attributedString: string1) 
     finalString.appendAttributedString(string2) 

     self.textView.textStorage?.setAttributedString(finalString) 

    } 

    func applicationWillTerminate(aNotification: NSNotification) { 
     // Insert code here to tear down your application 
    } 


    @IBAction func getAttributedString(sender:AnyObject){ 
     var attributedString = self.textView.attributedString() 
     print(attributedString) 
    } 
} 

enter image description here

1

NSLayoutManagerを介して達成することができるリッチテキスト編集機能及びフォーマット記憶が付属していソースを変更せずにテキストを表示する前にテキストを変更するための特別な機能:一時属性。彼らは、スペルチェックの間に言葉の緑/赤の下線などのインスタンスに使用されることになっている、または単に部分を強調するために

- (void)addTemporaryAttribute:(NSString *)attrName value:(id)value forCharacterRange:(NSRange)charRange 

の名前には「TemporaryAttribute」を持っている方法のためNSLayoutManagerドキュメントを検索しますテキストの一時的な元の帰属テキストソースを変更することなく、単なる表示目的のためにテキストの一部の外観を変更することができます。

+0

これは素晴らしい解決策のようです。私が考えた別の解決策は 'NSTextView'のメイン' NSTextLayer'レイヤーの下に 'CALayer'インスタンスを追加してハイライトを表示する方法でしたが、この解決策はより良く見えます。どう思いますか? – jamespick

関連する問題