Wrapper

class Wrapper: NSView, ChordProEditorDelegate

A wrapper for

  • NSScrollView
  • NSTextView
  • NSRulerView
  • Init the NSView

    Declaration

    Swift

    override init(frame frameRect: NSRect)

    Parameters

    frameRect

    The rect of the NSView

  • Init the NSView

    Declaration

    Swift

    required init?(coder: NSCoder)

    Parameters

    coder

    The NSCoder

  • The delegate for the text editor

    Declaration

    Swift

    weak var delegate: NSTextViewDelegate?
  • The scroll view of the text editor

    Declaration

    Swift

    private lazy var scrollView: NSScrollView = {
        let scrollView = NSScrollView()
        scrollView.drawsBackground = true
        scrollView.borderType = .noBorder
        scrollView.hasVerticalScroller = true
        scrollView.hasHorizontalScroller = false
        scrollView.hasHorizontalRuler = false
        scrollView.hasVerticalRuler = true
        scrollView.rulersVisible = true
        scrollView.autoresizingMask = [.width, .height]
        scrollView.translatesAutoresizingMaskIntoConstraints = false
    
        return scrollView
    }()
  • The actual text view

    Declaration

    Swift

    lazy var textView: TextView = {
        let contentSize = scrollView.contentSize
        let textStorage = NSTextStorage()
        let layoutManager = LayoutManager()
        textStorage.addLayoutManager(layoutManager)
    
        let textContainer = NSTextContainer(containerSize: scrollView.frame.size)
        textContainer.widthTracksTextView = true
        textContainer.containerSize = NSSize(
            width: contentSize.width,
            height: CGFloat.greatestFiniteMagnitude
        )
    
        layoutManager.addTextContainer(textContainer)
    
        let textView = TextView(frame: .zero, textContainer: textContainer)
        textView.autoresizingMask = .width
        textView.backgroundColor = NSColor.textBackgroundColor
        textView.delegate = self.delegate
        textView.font = .systemFont(ofSize: 8)
        textView.isEditable = true
        textView.isHorizontallyResizable = false
        textView.isVerticallyResizable = true
        textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
        textView.minSize = NSSize(width: 0, height: contentSize.height)
        textView.textColor = NSColor.labelColor
        textView.allowsUndo = true
        textView.isAutomaticQuoteSubstitutionEnabled = false
        textView.isAutomaticTextCompletionEnabled = false
        textView.isAutomaticTextReplacementEnabled = false
        textView.isAutomaticDashSubstitutionEnabled = false
        textView.isAutomaticSpellingCorrectionEnabled = false
        textView.layoutManager?.delegate = layoutManager
        textView.chordProEditorDelegate = self
        textView.textContainerInset = .init(width: 2, height: 0)
        textView.drawsBackground = false
    
        return textView
    }()
  • The NSRulerView for the line numbers

    Declaration

    Swift

    lazy private var lineNumbers = LineNumbersView()
  • Override viewWillDraw to setup the editor

    Declaration

    Swift

    override func viewWillDraw()
  • Setup the scroll view

    Declaration

    Swift

    func setupScrollViewConstraints()
  • Setup the text view

    Declaration

    Swift

    func setupTextView()

ChordProEditorDelegate