Swiftpack.co - Package - ChimeHQ/TextViewPlus

Github CI Carthage compatible


TextViewPlus is a collection of utilities for making it easier to work with NSTextView.


Swift Package Manager:

dependencies: [
    .package(url: "https://github.com/ChimeHQ/TextViewPlus.git")


github "ChimeHQ/TextViewPlus"



Wrappers around the underlying NSLayoutManager, but with a much more convenient API.

func enumerateLineFragments(for range: NSRange, block: (NSRect, NSRange) -> Void)
func enumerateLineFragments(for rect: NSRect, block: (NSRect, NSRange) -> Void)


Handy methods for computing ranges of text within the view.

func textRange(for rect: NSRect) -> NSRange
var visibleTextRange: NSRange


Convenience methods for computing selection ranges/locations.

var selectedTextRanges: [NSRange]
var selectedContinuousRange: NSRange?
var insertionLocation: Int?


Styling changes can be very expensive, this method is much faster in certain common cases.

func updateFont(_ newFont: NSFont, color newColor: NSColor)


Computing bounding rectangles of displayed text.

func boundingRect(for range: NSRange) -> NSRect?
func boundingRect(forGlyphRange range: NSRange) -> NSRect?
func boundingSelectionRects(forRange range: NSRange) -> [NSRect]

Attributed Strings

Programmtic modification of the underlying attributed string in the NSTextStorage, with support for delegate callbacks and undo.

func replaceCharacters(in range: NSRange, with attributedString: NSAttributedString)

// with undo supported
func replaceString(in range: NSRange, with attributedString: NSAttributedString)


Changing NSTextView behaviors can be tricky, and often involve complex interactions with the whole system (NSLayoutManager, NSTextContainer, NSScrollView, etc).

public var wrapsTextToHorizontalBounds: Bool


// Fixes a widely-seen selection drawing artifact
func applySelectionDrawingWorkaround()

Suggestions or Feedback

We'd love to hear from you! Get in touch via twitter, an issue, or a pull request.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.


Stars: 18


Used By

Total: 0


v1.0.3 - 2020-05-07 12:18:48

  • Improved documentation
  • Improvement text wrapping behavior

v1.0.2 - 2020-04-23 14:02:28

  • Workaround for selection drawing artifact
  • NSTextView property to control text wrapping behavior

v1.0.1 - 2020-01-07 15:27:53

Fix a pretty nasty bug where replaceString(in:with:) would crash on a number of perfectly acceptable inputs.

v1.0 - 2020-01-05 12:41:16

Initial release!