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)
Behavior
Changing NSTextView behaviors can be tricky, and often involve complex interactions with the whole system (NSLayoutManager, NSTextContainer, NSScrollView, etc).
public var wrapsTextToHorizontalBounds: Bool
Workarounds
// Fixes a widely-seen selection drawing artifact
func applySelectionDrawingWorkaround()
In versions of macOS before 13, TextKit 2 doesn’t correctly apply rendering attributes. You can sub in this NSTextLayoutFragment to workaround the issue.
extension YourClass: NSTextLayoutManagerDelegate {
func textLayoutManager(_ textLayoutManager: NSTextLayoutManager, textLayoutFragmentFor location: NSTextLocation, in textElement: NSTextElement) -> NSTextLayoutFragment {
let range = textElement.elementRange
switch textElement {
case let paragraph as NSTextParagraph:
return ParagraphRenderingAttributeTextLayoutFragment(textParagraph: paragraph, range: range)
default:
return NSTextLayoutFragment(textElement: textElement, range: range)
}
}
}
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.
TextViewPlus
TextViewPlus is a collection of utilities for making it easier to work with
NSTextView
and the text system.Integration
Swift Package Manager:
Extensions
Geometry
Wrappers around the underlying
NSLayoutManager
, but with a much more convenient API.Ranges
Handy methods for computing ranges of text within the view.
Selection
Convenience methods for computing selection ranges/locations.
Style
Styling changes can be very expensive, this method is much faster in certain common cases.
Bounding
Computing bounding rectangles of displayed text.
Attributed Strings
Programmtic modification of the underlying attributed string in the
NSTextStorage
, with support for delegate callbacks and undo.Behavior
Changing
NSTextView
behaviors can be tricky, and often involve complex interactions with the whole system (NSLayoutManager
,NSTextContainer
,NSScrollView
, etc).Workarounds
In versions of macOS before 13, TextKit 2 doesn’t correctly apply rendering attributes. You can sub in this
NSTextLayoutFragment
to workaround the issue.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.