├── Screenshot.gif
├── LNTextView.xcodeproj
├── project.xcworkspace
│ └── contents.xcworkspacedata
├── xcshareddata
│ └── xcschemes
│ │ └── LNTextView.xcscheme
└── project.pbxproj
├── LNTextView
├── LNTextView.h
├── Info.plist
├── LineHighlightingTextView.swift
├── LNTextView.swift
└── LineNumberView.swift
├── Example
├── AppDelegate.swift
├── Info.plist
├── ViewController.swift
├── Assets.xcassets
│ └── AppIcon.appiconset
│ │ └── Contents.json
└── Base.lproj
│ └── Main.storyboard
├── LICENSE
├── README.md
└── .gitignore
/Screenshot.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JonWorms/LNTextView/HEAD/Screenshot.gif
--------------------------------------------------------------------------------
/LNTextView.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LNTextView/LNTextView.h:
--------------------------------------------------------------------------------
1 | //
2 | // LNTextView.h
3 | // LNTextView
4 | //
5 | // Created by Jon Worms on 4/9/17.
6 | //
7 | //
8 |
9 | #import
10 |
11 | //! Project version number for LNTextView.
12 | FOUNDATION_EXPORT double LNTextViewVersionNumber;
13 |
14 | //! Project version string for LNTextView.
15 | FOUNDATION_EXPORT const unsigned char LNTextViewVersionString[];
16 |
17 | // In this header, you should import all the public headers of your framework using statements like #import
18 |
19 |
20 |
--------------------------------------------------------------------------------
/Example/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // Example
4 | //
5 | // Created by Jon Worms on 4/9/17.
6 | //
7 | //
8 |
9 | import Cocoa
10 |
11 | @NSApplicationMain
12 | class AppDelegate: NSObject, NSApplicationDelegate {
13 |
14 |
15 |
16 | func applicationDidFinishLaunching(_ aNotification: Notification) {
17 | // Insert code here to initialize your application
18 | }
19 |
20 | func applicationWillTerminate(_ aNotification: Notification) {
21 | // Insert code here to tear down your application
22 | }
23 |
24 |
25 | }
26 |
27 |
--------------------------------------------------------------------------------
/LNTextView/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | FMWK
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | $(CURRENT_PROJECT_VERSION)
21 | NSPrincipalClass
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/Example/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIconFile
10 |
11 | CFBundleIdentifier
12 | $(PRODUCT_BUNDLE_IDENTIFIER)
13 | CFBundleInfoDictionaryVersion
14 | 6.0
15 | CFBundleName
16 | $(PRODUCT_NAME)
17 | CFBundlePackageType
18 | APPL
19 | CFBundleShortVersionString
20 | 1.0
21 | CFBundleVersion
22 | 1
23 | LSMinimumSystemVersion
24 | $(MACOSX_DEPLOYMENT_TARGET)
25 | NSMainStoryboardFile
26 | Main
27 | NSPrincipalClass
28 | NSApplication
29 |
30 |
31 |
--------------------------------------------------------------------------------
/Example/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // Example
4 | //
5 | // Created by Jon Worms on 4/9/17.
6 | //
7 | //
8 |
9 | import Cocoa
10 | import LNTextView
11 |
12 | class ViewController: NSViewController {
13 |
14 | @IBOutlet var textView: LNTextView!
15 |
16 | override func viewDidLoad() {
17 | super.viewDidLoad()
18 |
19 | // Set a color theme:
20 | textView.textBackgroundColor = NSColor(calibratedRed: CGFloat(29.0/255.0), green: CGFloat(32.0/255.0), blue: CGFloat(35.0/255.0), alpha: 1)
21 | textView.lineNumbersBackgroundColor = NSColor(calibratedRed: CGFloat(54.0/255.0), green: CGFloat(56.0/255.0), blue: CGFloat(58.0/255.0), alpha: 1)
22 | textView.lineNumbersForegroundColor = NSColor.gray
23 | textView.selectionColor = NSColor(calibratedRed: 0.28, green: 0.30, blue: 0.32, alpha: 1)
24 | textView.currentLineColor = NSColor.white
25 | textView.textColor = NSColor.white
26 | // Do any additional setup after loading the view.
27 | }
28 |
29 | override var representedObject: Any? {
30 | didSet {
31 | // Update the view, if already loaded.
32 | }
33 | }
34 |
35 |
36 | }
37 |
38 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Jon Worms
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Example/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "mac",
5 | "size" : "16x16",
6 | "scale" : "1x"
7 | },
8 | {
9 | "idiom" : "mac",
10 | "size" : "16x16",
11 | "scale" : "2x"
12 | },
13 | {
14 | "idiom" : "mac",
15 | "size" : "32x32",
16 | "scale" : "1x"
17 | },
18 | {
19 | "idiom" : "mac",
20 | "size" : "32x32",
21 | "scale" : "2x"
22 | },
23 | {
24 | "idiom" : "mac",
25 | "size" : "128x128",
26 | "scale" : "1x"
27 | },
28 | {
29 | "idiom" : "mac",
30 | "size" : "128x128",
31 | "scale" : "2x"
32 | },
33 | {
34 | "idiom" : "mac",
35 | "size" : "256x256",
36 | "scale" : "1x"
37 | },
38 | {
39 | "idiom" : "mac",
40 | "size" : "256x256",
41 | "scale" : "2x"
42 | },
43 | {
44 | "idiom" : "mac",
45 | "size" : "512x512",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "mac",
50 | "size" : "512x512",
51 | "scale" : "2x"
52 | }
53 | ],
54 | "info" : {
55 | "version" : 1,
56 | "author" : "xcode"
57 | }
58 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | LNTextView
2 | ===========
3 |
4 | 
5 | ### Installing:
6 | You can either copy __LNTextView.swift__, __LineHighlightingTextView.swift__, and __LineNumberView.swift__ into your project, or you can build and embed the LNTextView framework into your project. A release will come soon, at which time you should be able to use [Carthage](https://github.com/Carthage/Carthage) as well.
7 | ### Example Usage:
8 | ###### With Storyboard:
9 | ```Swift
10 | //
11 | // ViewController.swift
12 | // Example
13 | //
14 | import Cocoa
15 | import LNTextView
16 |
17 | class ViewController: NSViewController {
18 |
19 | @IBOutlet var textView: LNTextView!
20 |
21 | override func viewDidLoad() {
22 | super.viewDidLoad()
23 |
24 | // Set a color theme:
25 | textView.textBackgroundColor = NSColor(calibratedRed: CGFloat(29.0/255.0), green: CGFloat(32.0/255.0), blue: CGFloat(35.0/255.0), alpha: 1)
26 | textView.lineNumbersBackgroundColor = NSColor(calibratedRed: CGFloat(54.0/255.0), green: CGFloat(56.0/255.0), blue: CGFloat(58.0/255.0), alpha: 1)
27 | textView.lineNumbersForegroundColor = NSColor.gray
28 | textView.selectionColor = NSColor(calibratedRed: 0.28, green: 0.30, blue: 0.32, alpha: 1)
29 | textView.currentLineColor = NSColor.white
30 | textView.textColor = NSColor.white
31 | // Do any additional setup after loading the view.
32 | }
33 |
34 | }
35 | ```
36 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4 |
5 | ## Build generated
6 | build/
7 | DerivedData/
8 |
9 | ## Various settings
10 | *.pbxuser
11 | !default.pbxuser
12 | *.mode1v3
13 | !default.mode1v3
14 | *.mode2v3
15 | !default.mode2v3
16 | *.perspectivev3
17 | !default.perspectivev3
18 | xcuserdata/
19 |
20 | ## Other
21 | *.moved-aside
22 | *.xcuserstate
23 |
24 | ## Obj-C/Swift specific
25 | *.hmap
26 | *.ipa
27 | *.dSYM.zip
28 | *.dSYM
29 |
30 | ## Playgrounds
31 | timeline.xctimeline
32 | playground.xcworkspace
33 |
34 | # Swift Package Manager
35 | #
36 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
37 | # Packages/
38 | .build/
39 |
40 | # CocoaPods
41 | #
42 | # We recommend against adding the Pods directory to your .gitignore. However
43 | # you should judge for yourself, the pros and cons are mentioned at:
44 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
45 | #
46 | # Pods/
47 |
48 | # Carthage
49 | #
50 | # Add this line if you want to avoid checking in source code from Carthage dependencies.
51 | # Carthage/Checkouts
52 |
53 | Carthage/Build
54 |
55 | # fastlane
56 | #
57 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
58 | # screenshots whenever they are needed.
59 | # For more information about the recommended setup visit:
60 | # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md
61 |
62 | fastlane/report.xml
63 | fastlane/Preview.html
64 | fastlane/screenshots
65 | fastlane/test_output
66 |
--------------------------------------------------------------------------------
/LNTextView.xcodeproj/xcshareddata/xcschemes/LNTextView.xcscheme:
--------------------------------------------------------------------------------
1 |
2 |
5 |
8 |
9 |
15 |
21 |
22 |
23 |
24 |
25 |
30 |
31 |
32 |
33 |
34 |
35 |
45 |
46 |
52 |
53 |
54 |
55 |
56 |
57 |
63 |
64 |
70 |
71 |
72 |
73 |
75 |
76 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/LNTextView/LineHighlightingTextView.swift:
--------------------------------------------------------------------------------
1 | //
2 | // LineHighlightingTextView.swift
3 | //
4 | // Copyright (c) 2017 Jon Worms
5 | //
6 | // Permission is hereby granted, free of charge, to any person obtaining a copy
7 | // of this software and associated documentation files (the "Software"), to deal
8 | // in the Software without restriction, including without limitation the rights
9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | // copies of the Software, and to permit persons to whom the Software is
11 | // furnished to do so, subject to the following conditions:
12 | //
13 | // The above copyright notice and this permission notice shall be included in all
14 | // copies or substantial portions of the Software.
15 | //
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | // SOFTWARE.
23 | //
24 |
25 | import Cocoa
26 | //TODO: Find out what this is: kATSULineHighlightCGColorTag
27 |
28 |
29 | protocol LineHighlightingTextViewDelegate {
30 | func selectionNeedsDisplay()
31 | }
32 |
33 |
34 | public class LineHighlightingTextView: NSTextView {
35 |
36 |
37 | var highlightingDelegate: LineHighlightingTextViewDelegate?
38 |
39 | var currentLineColor: NSColor = NSColor(calibratedRed: 0.96, green: 0.96, blue: 0.97, alpha: 1)
40 | var selectionColor: NSColor {
41 | set { selectedTextAttributes[NSBackgroundColorAttributeName] = newValue }
42 | get { return selectedTextAttributes[NSBackgroundColorAttributeName] as! NSColor }
43 | }
44 |
45 |
46 |
47 |
48 | override init(frame frameRect: NSRect) {
49 | super.init(frame: frameRect)
50 | setup()
51 | }
52 |
53 | override init(frame frameRect: NSRect, textContainer container: NSTextContainer?) {
54 | super.init(frame: frameRect, textContainer: container)
55 | setup()
56 | }
57 |
58 | required public init?(coder: NSCoder) {
59 | fatalError("init(coder:) has not been implemented")
60 | }
61 |
62 | private func setup() {
63 | selectionColor = NSColor(calibratedRed: 0.69, green: 0.84, blue: 1.0, alpha: 1.0)
64 | }
65 |
66 |
67 | override public var drawsBackground: Bool {
68 | set {} // always return false, we'll draw the background
69 | get { return false }
70 | }
71 |
72 |
73 | var selectedLineRect: NSRect? {
74 | guard let layout = layoutManager,
75 | let container = textContainer,
76 | let text = textStorage else { return nil }
77 |
78 | if selectedRange().length > 0 { return nil }
79 |
80 | return layout.boundingRect(forGlyphRange: text.rangeOfLineAtLocation(selectedRange().location), in: container)
81 | }
82 |
83 | override public func draw(_ dirtyRect: NSRect) {
84 | guard let context = NSGraphicsContext.current()?.cgContext else { return }
85 |
86 | context.setFillColor(backgroundColor.cgColor)
87 | context.fill(dirtyRect)
88 |
89 | if let textRect = selectedLineRect {
90 | let lineRect = NSRect(x: 0, y: textRect.origin.y, width: dirtyRect.width, height: textRect.height)
91 | context.setFillColor(currentLineColor.cgColor)
92 | context.fill(lineRect)
93 | }
94 |
95 | super.draw(dirtyRect)
96 |
97 | }
98 |
99 | /*
100 | override func setSelectedRange(_ charRange: NSRange) {
101 | super.setSelectedRange(charRange)
102 | needsDisplay = true
103 | highlightingDelegate?.selectionNeedsDisplay()
104 | }*/
105 |
106 |
107 | override public func setSelectedRange(_ charRange: NSRange, affinity: NSSelectionAffinity, stillSelecting stillSelectingFlag: Bool) {
108 | super.setSelectedRange(charRange, affinity: affinity, stillSelecting: stillSelectingFlag)
109 | needsDisplay = true
110 | highlightingDelegate?.selectionNeedsDisplay()
111 | }
112 |
113 |
114 | }
115 |
116 |
117 |
118 |
119 |
120 | extension UnicodeScalar {
121 | var isWhitespace: Bool {
122 | return NSCharacterSet.whitespaces.contains(self) || NSCharacterSet.newlines.contains(self)
123 | }
124 |
125 | var isNewline: Bool {
126 | return NSCharacterSet.newlines.contains(self)
127 | }
128 | }
129 |
130 | extension String.UnicodeScalarView {
131 | subscript(index: Int) -> UnicodeScalar {
132 | var i = self.startIndex
133 | self.formIndex(&i, offsetBy: index)
134 | return self[i]
135 | }
136 | }
137 |
138 |
139 | extension NSAttributedString {
140 |
141 | ///
142 | /// Returns an NSRange containing the argument location that starts after
143 | /// a newline (or the beginning of the string) and ends at a new line (or
144 | /// the end of the string)
145 | ///
146 | func rangeOfLineAtLocation(_ location: Int) -> NSRange {
147 | let scalars = string.unicodeScalars
148 |
149 | if scalars[location].isNewline {
150 | var start = location
151 | while(start > 0 && !scalars[start-1].isNewline) {
152 | start -= 1
153 | }
154 | return NSMakeRange(start, location-start)
155 | }
156 |
157 | var start: Int = location
158 | while(start > 0 && !scalars[start-1].isNewline) {
159 | start -= 1
160 | }
161 |
162 | var end = location
163 | while(end < scalars.count-1 && !scalars[end+1].isNewline) {
164 | end += 1
165 | }
166 |
167 | return NSMakeRange(start, end-start)
168 | }
169 |
170 |
171 | }
172 |
--------------------------------------------------------------------------------
/LNTextView/LNTextView.swift:
--------------------------------------------------------------------------------
1 | //
2 | // LNTextView.swift
3 | //
4 | // Copyright (c) 2017 Jon Worms
5 | //
6 | // Permission is hereby granted, free of charge, to any person obtaining a copy
7 | // of this software and associated documentation files (the "Software"), to deal
8 | // in the Software without restriction, including without limitation the rights
9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | // copies of the Software, and to permit persons to whom the Software is
11 | // furnished to do so, subject to the following conditions:
12 | //
13 | // The above copyright notice and this permission notice shall be included in all
14 | // copies or substantial portions of the Software.
15 | //
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | // SOFTWARE.
23 | //
24 |
25 | import Cocoa
26 |
27 | let testColors: [NSColor] = [.yellow, .blue, .red, .orange, .green, .purple]
28 |
29 |
30 | public class LNTextView: NSView, NSTextStorageDelegate, NSTextViewDelegate, LineHighlightingTextViewDelegate {
31 |
32 |
33 | // MARK: Colors
34 | public var textBackgroundColor: NSColor {
35 | set { _textView.backgroundColor = newValue }
36 | get { return _textView.backgroundColor }
37 | }
38 |
39 | public var lineNumbersBackgroundColor: NSColor {
40 | set { _lineNumbers.backgroundColor = newValue }
41 | get { return _lineNumbers.backgroundColor }
42 | }
43 |
44 | public var lineNumbersForegroundColor: NSColor {
45 | set { _lineNumbers.foregroundColor = newValue }
46 | get { return _lineNumbers.foregroundColor }
47 | }
48 |
49 | public var currentLineColor: NSColor {
50 | set {
51 | _lineNumbers.selectionColor = newValue
52 | _textView.currentLineColor = newValue.withAlphaComponent(0.1)
53 | }
54 | get { return _lineNumbers.selectionColor }
55 | }
56 |
57 | public var selectionColor: NSColor {
58 | set { _textView.selectedTextAttributes[NSBackgroundColorAttributeName] = newValue }
59 | get { return _textView.selectedTextAttributes[NSBackgroundColorAttributeName] as! NSColor }
60 | }
61 |
62 | public var textColor: NSColor {
63 | set {
64 | _textView.textColor = newValue
65 | _textView.insertionPointColor = newValue
66 | }
67 | get { return _textView.textColor ?? NSColor.black }
68 | }
69 |
70 | public var font: NSFont? {
71 | set { _textView.font = newValue }
72 | get { return _textView.font }
73 | }
74 |
75 |
76 |
77 | public var selectedRanges: [NSValue] { return _textView.selectedRanges }
78 |
79 |
80 | private var _textView: LineHighlightingTextView!
81 |
82 | public var storageDelegate: NSTextStorageDelegate?
83 |
84 |
85 | private var _lineNumbers: LineNumberView!
86 | private var _scrollView: NSScrollView!
87 | private var _textStorage: NSTextStorage { return _textView.textStorage! }
88 |
89 |
90 | override init(frame frameRect: NSRect) {
91 | super.init(frame: frameRect)
92 | setup()
93 | }
94 |
95 | required public init?(coder: NSCoder) {
96 | super.init(coder: coder)
97 | setup()
98 | }
99 |
100 | private func setup() {
101 |
102 | _scrollView = NSScrollView(frame: self.bounds)
103 |
104 | _scrollView.hasVerticalScroller = true
105 | _scrollView.hasHorizontalScroller = false
106 | _scrollView.hasVerticalRuler = true
107 | _scrollView.rulersVisible = true
108 |
109 | _scrollView.borderType = .noBorder
110 | _scrollView.autoresizingMask = [.viewWidthSizable , .viewHeightSizable]
111 |
112 |
113 | _textView = LineHighlightingTextView(frame: NSRect(origin: NSZeroPoint, size: _scrollView.contentSize))
114 | _textView.minSize = NSSize(width: 0, height: _scrollView.contentSize.height)
115 | _textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude,height: CGFloat.greatestFiniteMagnitude)
116 | _textView.isVerticallyResizable = true
117 | _textView.isHorizontallyResizable = false
118 | _textView.autoresizingMask = .viewWidthSizable
119 | _textView.textContainer?.containerSize = NSSize(width: _scrollView.contentSize.width, height: CGFloat.greatestFiniteMagnitude)
120 | _textView.textContainer?.widthTracksTextView = true
121 | _textView.highlightingDelegate = self
122 |
123 | _lineNumbers = LineNumberView(frame: NSRect(x: 0, y: 0, width: 50, height: 0))
124 | _lineNumbers.scrollView = _scrollView
125 | _lineNumbers.orientation = .verticalRuler
126 | _lineNumbers.clientView = _textView
127 |
128 |
129 | _scrollView.verticalRulerView = _lineNumbers
130 | _scrollView.documentView = _textView
131 |
132 |
133 | _textStorage.delegate = self
134 | _textView.delegate = self
135 |
136 | addSubview(_scrollView)
137 |
138 | font = NSFont(name: "Menlo-Regular", size: 11)
139 | }
140 |
141 |
142 |
143 | public func textStorage(_ textStorage: NSTextStorage, willProcessEditing editedMask: NSTextStorageEditActions, range editedRange: NSRange, changeInLength delta: Int) {
144 | _lineNumbers.needsDisplay = true
145 | storageDelegate?.textStorage?(textStorage, willProcessEditing: editedMask, range: editedRange, changeInLength: delta)
146 | }
147 |
148 |
149 |
150 | public func textStorage(_ textStorage: NSTextStorage, didProcessEditing editedMask: NSTextStorageEditActions, range editedRange: NSRange, changeInLength delta: Int) {
151 | storageDelegate?.textStorage?(textStorage, didProcessEditing: editedMask, range: editedRange, changeInLength: delta)
152 | }
153 |
154 |
155 | override public func layout() {
156 | super.layout();
157 | _lineNumbers.needsDisplay = true
158 | }
159 |
160 | func selectionNeedsDisplay() {
161 | _lineNumbers.needsDisplay = true
162 | }
163 |
164 |
165 | }
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
--------------------------------------------------------------------------------
/LNTextView/LineNumberView.swift:
--------------------------------------------------------------------------------
1 | //
2 | // LineNumberView.swift
3 | //
4 | // Copyright (c) 2017 Jon Worms
5 | //
6 | // Permission is hereby granted, free of charge, to any person obtaining a copy
7 | // of this software and associated documentation files (the "Software"), to deal
8 | // in the Software without restriction, including without limitation the rights
9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | // copies of the Software, and to permit persons to whom the Software is
11 | // furnished to do so, subject to the following conditions:
12 | //
13 | // The above copyright notice and this permission notice shall be included in all
14 | // copies or substantial portions of the Software.
15 | //
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | // SOFTWARE.
23 | //
24 |
25 | import Cocoa
26 |
27 |
28 | public class LineNumberView: NSRulerView {
29 |
30 |
31 | private var fontAttributes: [String: AnyObject] = [:]
32 |
33 |
34 | // MARK: Colors
35 | var backgroundColor: NSColor = NSColor(calibratedRed: 0.97, green: 0.97, blue: 0.97, alpha: 1.0)
36 | var foregroundColor: NSColor {
37 | set { fontAttributes[NSForegroundColorAttributeName] = newValue }
38 | get { return fontAttributes[NSForegroundColorAttributeName] as! NSColor }
39 | }
40 | var selectionColor: NSColor = NSColor.black
41 |
42 |
43 |
44 |
45 | required override public init(scrollView: NSScrollView?, orientation: NSRulerOrientation) {
46 | super.init(scrollView: scrollView, orientation: orientation)
47 | let lineNumberStyle = NSMutableParagraphStyle()
48 | lineNumberStyle.alignment = .right
49 |
50 | fontAttributes[NSParagraphStyleAttributeName] = lineNumberStyle
51 | fontAttributes[NSBackgroundColorAttributeName] = NSColor.clear
52 | foregroundColor = NSColor(calibratedRed: 0.65, green: 0.65, blue: 0.65, alpha: 1.0)
53 | }
54 |
55 | required public init(coder: NSCoder) {
56 | fatalError("init(coder:) has not been implemented")
57 | }
58 |
59 |
60 |
61 |
62 |
63 | override public var isFlipped: Bool { return true }
64 |
65 |
66 | override public func draw(_ dirtyRect: NSRect) {
67 | guard let context: CGContext = NSGraphicsContext.current()?.cgContext else { return }
68 |
69 | // fill the background
70 | context.setFillColor(backgroundColor.cgColor)
71 | context.fill(dirtyRect)
72 |
73 | // draw a border on the right
74 | context.setStrokeColor(foregroundColor.cgColor)
75 | context.setLineWidth(0.5)
76 | context.move(to: CGPoint(x: dirtyRect.width, y: 0))
77 | context.addLine(to: CGPoint(x: dirtyRect.width, y: dirtyRect.height))
78 | context.strokePath()
79 |
80 | // this usually gets called on super.draw(dirtyRect), but we're not calling it
81 | drawHashMarksAndLabels(in: dirtyRect)
82 | }
83 |
84 |
85 |
86 |
87 | override public func drawHashMarksAndLabels(in rect: NSRect) {
88 |
89 | guard let textView: LineHighlightingTextView = self.clientView as? LineHighlightingTextView,
90 | let textContainer: NSTextContainer = textView.textContainer,
91 | let textStorage: NSTextStorage = textView.textStorage,
92 | let layout: NSLayoutManager = textView.layoutManager,
93 | let context: CGContext = NSGraphicsContext.current()?.cgContext else {
94 | return
95 | }
96 |
97 |
98 |
99 | // scalar values for text view content
100 | let scalars = textStorage.string.unicodeScalars
101 |
102 | // range of glyphs in the visible area of the text view
103 | let visibleGlyphRange = layout.glyphRange(forBoundingRect: textView.visibleRect, in: textContainer)
104 | let selectedLinePosition: CGFloat = textView.selectedLineRect?.origin.y ?? -1
105 |
106 | var lineNumber: Int = 1
107 | // count newlines in range up to the visible range
108 | for i in 0..= fontBaselineOffset {
122 | fontBaselineOffset = ceil(fontBaselineOffset)
123 | } else {
124 | fontBaselineOffset = floor(fontBaselineOffset)
125 | }
126 | let lineOffset = layout.defaultBaselineOffset(for: font) - fontBaselineOffset
127 | // NOTE: ^ above is close, but needs work
128 |
129 |
130 | // translate vertically to line up with document position and baseline offset
131 | context.translateBy(x: 0, y: convert(NSZeroPoint, from: textView).y + lineOffset)
132 |
133 |
134 |
135 | // Begin drawing line numbers:
136 |
137 | // y-position of the last line
138 | var lastLinePosition: CGFloat = 0
139 |
140 |
141 | // range of each line as we step through the visible Range, starting at the start of the visible range
142 | var lineStart = visibleGlyphRange.location
143 | var lineLength = 0
144 |
145 |
146 | for i in visibleGlyphRange.location..
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
654 |
655 |
656 |
657 |
658 |
659 |
660 |
661 |
662 |
663 |
664 |
665 |
666 |
667 |
668 |
669 |
670 |
671 |
672 |
673 |
674 |
675 |
676 |
677 |
678 |
679 |
680 |
681 |
682 |
683 |
684 |
685 |
686 |
687 |
688 |
689 |
690 |
691 |
692 |
693 |
694 |
695 |
696 |
697 |
698 |
699 |
--------------------------------------------------------------------------------