2016-02-09 34 views
7

我的目标是制作一个类似于Goole Docs文本编辑器的视图,其中注释突出显示评论后面的文本。 HighlightNSView和NSScrollView中的NSTextView

我的解决办法是将具有含NSView(设定为文档视图)的NSScrollView该卷轴和既包含文本和其他NSView s表示将成为亮点的NSTextView

要使用此功能,NSTextView的尺寸必须与其直接属于NSScrollView一样。但是,我不能让NSTextView有这种行为。

我对布局的代码是:

LatinViewController:的loadView()

... 
let latinView = LatinView() 
latinView.autoresizingMask = [.ViewWidthSizable] 
latinView.wantsLayer = true 
latinView.layer?.backgroundColor = Theme.greenColor().CGColor 
self.latinView = latinView 
scrollView.documentView = 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来实现功能。以下是示例代码 一个NSTextView已经自带了丰富的文本编辑功能和格式存储可通过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) 
    } 
} 
(一般所使用的 NSTextView,并 NSAttributedString

enter image description here

1

NSLayoutManager实现具有这是一个专门用于在屏幕上显示文本之前更改文本而不更改源代码的特殊功能:临时属性。

查找NSLayoutManager文档为拥有“TemporaryAttribute”在他们的名字的方法:

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

他们应该拼写检查过程中使用,例如作为词的绿/红色下划线,或简单地突出的部分的文字暂时。它们允许您修改仅用于显示目的的文本部分的外观,而无需修改原始属性文本源。

+0

这看起来像一个很好的解决方案。我想到的另一个解决方案是在NSTextView的主NSTextLayer层下添加CALayer实例来显示突出显示,但这个解决方案看起来更好。你怎么看? – jamespick

相关问题