Skip to content

Commit 51d0924

Browse files
Text wrapping, selection artifact suppression
1 parent f878527 commit 51d0924

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

TextViewPlus.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

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

3233
/* Begin PBXFileReference section */
34+
C96AF30E244E1872004EB905 /* NSTextView+Workarounds.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSTextView+Workarounds.swift"; sourceTree = "<group>"; };
3335
C9C7D7C323BF6CCC00282686 /* TextViewPlus.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = TextViewPlus.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3436
C9C7D7C623BF6CCC00282686 /* TextViewPlus.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TextViewPlus.h; sourceTree = "<group>"; };
3537
C9C7D7C723BF6CCC00282686 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -95,6 +97,7 @@
9597
C9C7D7E523BF6D8C00282686 /* NSTextView+Bounding.swift */,
9698
C9C7D7E823BF6F6900282686 /* NSTextView+AttributedString.swift */,
9799
C9C7D7E723BF6E8400282686 /* TextViewPlus.xcconfig */,
100+
C96AF30E244E1872004EB905 /* NSTextView+Workarounds.swift */,
98101
);
99102
path = TextViewPlus;
100103
sourceTree = "<group>";
@@ -241,6 +244,7 @@
241244
isa = PBXSourcesBuildPhase;
242245
buildActionMask = 2147483647;
243246
files = (
247+
C96AF30F244E1872004EB905 /* NSTextView+Workarounds.swift in Sources */,
244248
C9C7D7E223BF6D3800282686 /* NSTextView+Ranges.swift in Sources */,
245249
C9C7D7E323BF6D3800282686 /* NSTextView+Selection.swift in Sources */,
246250
C9C7D7E623BF6D8C00282686 /* NSTextView+Bounding.swift in Sources */,

TextViewPlus/NSTextView+Ranges.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import Foundation
1010
import class AppKit.NSTextView
1111

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

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

32-
@objc open var visibleTextRange: NSRange {
32+
var visibleTextRange: NSRange {
3333
return textRange(for: visibleRect)
3434
}
3535
}

TextViewPlus/NSTextView+Style.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,23 @@ extension NSTextView {
3333

3434
textStorage?.endEditing()
3535
}
36+
37+
public var wrapsTextToHorizontalBounds: Bool {
38+
get {
39+
return textContainer?.widthTracksTextView ?? false
40+
}
41+
set {
42+
guard let scrollView = enclosingScrollView else {
43+
return
44+
}
45+
46+
let max = CGFloat.greatestFiniteMagnitude
47+
let width = newValue ? scrollView.contentSize.width : max
48+
let size = NSSize(width: width, height: max)
49+
50+
textContainer?.containerSize = size
51+
textContainer?.widthTracksTextView = newValue
52+
isHorizontallyResizable = newValue == false
53+
}
54+
}
3655
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// NSTextView+Workarounds.swift
3+
// TextViewPlus
4+
//
5+
// Created by Matt Massicotte on 2020-04-20.
6+
// Copyright © 2020 ChimeHq. All rights reserved.
7+
//
8+
9+
import Cocoa
10+
11+
extension NSTextView {
12+
13+
/// Fixes selection drawing artifact issues.
14+
///
15+
/// In many circumstances, NSTextView will perform incorrect drawing of selection. This
16+
/// will result in visual artifacts left over on the text background. This problem is
17+
/// visible in most applications that use NSTextView, including ones shipped by Apple.
18+
///
19+
/// After some trial and error, it was determined that performing this operation will
20+
/// eliminate the visual artifacts and have limited (or possibly no) performance impact.
21+
public func applySelectionDrawingWorkaround() {
22+
rotate(byDegrees: 1.0)
23+
rotate(byDegrees: -1.0)
24+
}
25+
}

TextViewPlus/TextViewPlus.xcconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
PRODUCT_NAME = TextViewPlus
1313
PRODUCT_BUNDLE_IDENTIFIER = com.chimehq.TextViewPlus
1414
PRODUCT_MODULE_NAME = TextViewPlus
15-
CURRENT_PROJECT_VERSION = 3
16-
MARKETING_VERSION = 1.0.1
15+
CURRENT_PROJECT_VERSION = 4
16+
MARKETING_VERSION = 1.0.2
1717

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

0 commit comments

Comments
 (0)