Skip to content

Commit

Permalink
Text wrapping, selection artifact suppression
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Apr 23, 2020
1 parent f878527 commit 51d0924
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
4 changes: 4 additions & 0 deletions TextViewPlus.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
C96AF30F244E1872004EB905 /* NSTextView+Workarounds.swift in Sources */ = {isa = PBXBuildFile; fileRef = C96AF30E244E1872004EB905 /* NSTextView+Workarounds.swift */; };
C9C7D7CD23BF6CCD00282686 /* TextViewPlus.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C9C7D7C323BF6CCC00282686 /* TextViewPlus.framework */; };
C9C7D7D223BF6CCD00282686 /* TextViewPlusTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9C7D7D123BF6CCD00282686 /* TextViewPlusTests.swift */; };
C9C7D7D423BF6CCD00282686 /* TextViewPlus.h in Headers */ = {isa = PBXBuildFile; fileRef = C9C7D7C623BF6CCC00282686 /* TextViewPlus.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -30,6 +31,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C96AF30E244E1872004EB905 /* NSTextView+Workarounds.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSTextView+Workarounds.swift"; sourceTree = "<group>"; };
C9C7D7C323BF6CCC00282686 /* TextViewPlus.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TextViewPlus.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C9C7D7C623BF6CCC00282686 /* TextViewPlus.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TextViewPlus.h; sourceTree = "<group>"; };
C9C7D7C723BF6CCC00282686 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -95,6 +97,7 @@
C9C7D7E523BF6D8C00282686 /* NSTextView+Bounding.swift */,
C9C7D7E823BF6F6900282686 /* NSTextView+AttributedString.swift */,
C9C7D7E723BF6E8400282686 /* TextViewPlus.xcconfig */,
C96AF30E244E1872004EB905 /* NSTextView+Workarounds.swift */,
);
path = TextViewPlus;
sourceTree = "<group>";
Expand Down Expand Up @@ -241,6 +244,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C96AF30F244E1872004EB905 /* NSTextView+Workarounds.swift in Sources */,
C9C7D7E223BF6D3800282686 /* NSTextView+Ranges.swift in Sources */,
C9C7D7E323BF6D3800282686 /* NSTextView+Selection.swift in Sources */,
C9C7D7E623BF6D8C00282686 /* NSTextView+Bounding.swift in Sources */,
Expand Down
6 changes: 3 additions & 3 deletions TextViewPlus/NSTextView+Ranges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import Foundation
import class AppKit.NSTextView

extension NSTextView {
@objc open func textRange(for rect: NSRect) -> NSRange {
public extension NSTextView {
func textRange(for rect: NSRect) -> NSRange {
let length = self.textStorage?.length ?? 0

guard let layoutManager = self.layoutManager else {
Expand All @@ -29,7 +29,7 @@ extension NSTextView {
return layoutManager.characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil)
}

@objc open var visibleTextRange: NSRange {
var visibleTextRange: NSRange {
return textRange(for: visibleRect)
}
}
19 changes: 19 additions & 0 deletions TextViewPlus/NSTextView+Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,23 @@ extension NSTextView {

textStorage?.endEditing()
}

public var wrapsTextToHorizontalBounds: Bool {
get {
return textContainer?.widthTracksTextView ?? false
}
set {
guard let scrollView = enclosingScrollView else {
return
}

let max = CGFloat.greatestFiniteMagnitude
let width = newValue ? scrollView.contentSize.width : max
let size = NSSize(width: width, height: max)

textContainer?.containerSize = size
textContainer?.widthTracksTextView = newValue
isHorizontallyResizable = newValue == false
}
}
}
25 changes: 25 additions & 0 deletions TextViewPlus/NSTextView+Workarounds.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// NSTextView+Workarounds.swift
// TextViewPlus
//
// Created by Matt Massicotte on 2020-04-20.
// Copyright © 2020 ChimeHq. All rights reserved.
//

import Cocoa

extension NSTextView {

/// Fixes selection drawing artifact issues.
///
/// In many circumstances, NSTextView will perform incorrect drawing of selection. This
/// will result in visual artifacts left over on the text background. This problem is
/// visible in most applications that use NSTextView, including ones shipped by Apple.
///
/// After some trial and error, it was determined that performing this operation will
/// eliminate the visual artifacts and have limited (or possibly no) performance impact.
public func applySelectionDrawingWorkaround() {
rotate(byDegrees: 1.0)
rotate(byDegrees: -1.0)
}
}
4 changes: 2 additions & 2 deletions TextViewPlus/TextViewPlus.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
PRODUCT_NAME = TextViewPlus
PRODUCT_BUNDLE_IDENTIFIER = com.chimehq.TextViewPlus
PRODUCT_MODULE_NAME = TextViewPlus
CURRENT_PROJECT_VERSION = 3
MARKETING_VERSION = 1.0.1
CURRENT_PROJECT_VERSION = 4
MARKETING_VERSION = 1.0.2

INFOPLIST_FILE = TextViewPlus/Info.plist
FRAMEWORK_SEARCH_PATHS = $(PROJECT_DIR)/Carthage/Build/Mac $(PROJECT_DIR)/Carthage/Build/Mac/Static
Expand Down

0 comments on commit 51d0924

Please sign in to comment.