Skip to content

Commit 5ad6503

Browse files
Textkit 2: Fixed gesture recognizer so tapping into text does not fallback to text kit 1 (#1691)
2 parents c05ea3b + 2fde51e commit 5ad6503

File tree

6 files changed

+54
-8
lines changed

6 files changed

+54
-8
lines changed

Simplenote.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@
541541
BAB6C04726BA4CAF007495C4 /* WidgetController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB6C04626BA4CAF007495C4 /* WidgetController.swift */; };
542542
BAB898D32BEC404200E238B8 /* CreateNewNoteIntentHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB898D22BEC404200E238B8 /* CreateNewNoteIntentHandler.swift */; };
543543
BABB22DF2D14DA6600FCF47D /* SPTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABB22DE2D14DA6300FCF47D /* SPTextView.swift */; };
544+
BABB22E12D162E6D00FCF47D /* NSTextLayoutManager+Simplenote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABB22E02D162E6200FCF47D /* NSTextLayoutManager+Simplenote.swift */; };
545+
BABB22E32D162E8400FCF47D /* NSTextLayoutFragment.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABB22E22D162E7D00FCF47D /* NSTextLayoutFragment.swift */; };
544546
BABFFF2226CF9094003A4C25 /* WidgetDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABFFF2126CF9094003A4C25 /* WidgetDefaults.swift */; };
545547
BABFFF2326CF9094003A4C25 /* WidgetDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABFFF2126CF9094003A4C25 /* WidgetDefaults.swift */; };
546548
BABFFF2426CF9094003A4C25 /* WidgetDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABFFF2126CF9094003A4C25 /* WidgetDefaults.swift */; };
@@ -1244,6 +1246,8 @@
12441246
BAB6C04626BA4CAF007495C4 /* WidgetController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetController.swift; sourceTree = "<group>"; };
12451247
BAB898D22BEC404200E238B8 /* CreateNewNoteIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateNewNoteIntentHandler.swift; sourceTree = "<group>"; };
12461248
BABB22DE2D14DA6300FCF47D /* SPTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPTextView.swift; sourceTree = "<group>"; };
1249+
BABB22E02D162E6200FCF47D /* NSTextLayoutManager+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSTextLayoutManager+Simplenote.swift"; sourceTree = "<group>"; };
1250+
BABB22E22D162E7D00FCF47D /* NSTextLayoutFragment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSTextLayoutFragment.swift; sourceTree = "<group>"; };
12471251
BABFFF2126CF9094003A4C25 /* WidgetDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetDefaults.swift; sourceTree = "<group>"; };
12481252
BAD0F1EC2BED49C200E73E45 /* FindNoteIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindNoteIntentHandler.swift; sourceTree = "<group>"; };
12491253
BAE08625261282D1009D40CD /* Note+Publish.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Note+Publish.swift"; sourceTree = "<group>"; };
@@ -1879,6 +1883,8 @@
18791883
E21F57B817C1244E001F02D3 /* SPEditorTextView.m */,
18801884
A6BBDA45255034E6005C8343 /* SPEditorTextView+Simplenote.swift */,
18811885
B52E2B142537480A0074509A /* SPEditorTapRecognizerDelegate.swift */,
1886+
BABB22E02D162E6200FCF47D /* NSTextLayoutManager+Simplenote.swift */,
1887+
BABB22E22D162E7D00FCF47D /* NSTextLayoutFragment.swift */,
18821888
);
18831889
name = Editor;
18841890
sourceTree = "<group>";
@@ -3471,6 +3477,7 @@
34713477
A60DF30825A44F0F00FDADF3 /* PinLockRemoveController.swift in Sources */,
34723478
A694ABAB25D1549D00CC3A2D /* FileStorage.swift in Sources */,
34733479
B513FB2422EF6A4B00B178AC /* SPUserInterface.swift in Sources */,
3480+
BABB22E32D162E8400FCF47D /* NSTextLayoutFragment.swift in Sources */,
34743481
B5DF734022A54EE800602CE7 /* SPDefaultTableViewCell.swift in Sources */,
34753482
B5796549250684EC00DFD6F7 /* EditorFactory.swift in Sources */,
34763483
A6C2721825AF0C1E00593731 /* TagListViewCell.swift in Sources */,
@@ -3542,6 +3549,7 @@
35423549
A6CDF900256B9CB900CF2F27 /* ViewSpinner.swift in Sources */,
35433550
375D24B721E01131007AB25A /* stack.c in Sources */,
35443551
373AD30821C4739500A4EA89 /* NSMutableAttributedString+Styling.m in Sources */,
3552+
BABB22E12D162E6D00FCF47D /* NSTextLayoutManager+Simplenote.swift in Sources */,
35453553
B524AE112352CC7900EA11D4 /* UIScreen+Simplenote.swift in Sources */,
35463554
A68A4345256FEE3000D1CA5D /* SPSettingsViewController+Extensions.swift in Sources */,
35473555
B543C7E523CF775C00003A80 /* NSSortDescriptor+Simplenote.swift in Sources */,

Simplenote/Classes/SPTextView.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@
2020
@implementation SPTextView
2121

2222
- (instancetype)init {
23-
2423
SPInteractiveTextStorage *textStorage = [[SPInteractiveTextStorage alloc] init];
25-
2624
NSTextContainer *container = [self setupTextContainerWith:textStorage];
2725

2826
self = [super initWithFrame:CGRectZero textContainer:container];
2927
if (self) {
3028
self.interactiveTextStorage = textStorage;
31-
3229
/*
3330
Issue #188:
3431
===========

Simplenote/Classes/UIGestureRecognizer+Simplenote.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@ extension UIGestureRecognizer {
2121
locationInContainer.x -= textView.textContainerInset.left
2222
locationInContainer.y -= textView.textContainerInset.top
2323

24-
return textView.layoutManager.characterIndex(for: locationInContainer,
25-
in: textView.textContainer,
26-
fractionOfDistanceBetweenInsertionPoints: nil)
24+
guard #available(iOS 17.0, *),
25+
let textLayoutManager = textView.textLayoutManager else {
26+
// TextKit 1 fallback
27+
return textView.layoutManager.characterIndex(
28+
for: locationInContainer,
29+
in: textView.textContainer,
30+
fractionOfDistanceBetweenInsertionPoints: nil)
31+
}
32+
33+
// TextKit 2
34+
return textLayoutManager.characterIndex(for: locationInContainer) ?? .zero
2735
}
2836
}

Simplenote/NSTextLayoutFragment.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// NSTextLayoutFragment.swift
3+
// Simplenote
4+
//
5+
// Created by Charlie Scheer on 12/20/24.
6+
// Copyright © 2024 Automattic. All rights reserved.
7+
//
8+
9+
extension NSTextLayoutFragment {
10+
@available(iOS 17.0, *)
11+
func characterIndex(for location: CGPoint) -> Int? {
12+
guard let lineFragment = textLineFragment(forVerticalOffset: location.y, requiresExactMatch: true) else {
13+
return nil
14+
}
15+
return lineFragment.characterIndex(for: location)
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// NSTextLayoutManager+Simplenote.swift
3+
// Simplenote
4+
//
5+
// Created by Charlie Scheer on 12/20/24.
6+
// Copyright © 2024 Automattic. All rights reserved.
7+
//
8+
9+
extension NSTextLayoutManager {
10+
@available(iOS 17.0, *)
11+
func characterIndex(for location: CGPoint) -> Int? {
12+
guard let lineFragment = textLayoutFragment(for: location) else {
13+
return nil
14+
}
15+
return lineFragment.characterIndex(for: location)
16+
}
17+
}

Simplenote/SPTextView.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ extension SPTextView {
1313
container.widthTracksTextView = true
1414
container.heightTracksTextView = true
1515

16-
1716
if #available(iOS 16.0, *) {
1817
let textLayoutManager = NSTextLayoutManager()
1918
let contentStorage = NSTextContentStorage()
@@ -35,7 +34,7 @@ extension SPTextView {
3534
//
3635
extension SPTextView: NSTextContentStorageDelegate {
3736
public func textContentStorage(_ textContentStorage: NSTextContentStorage, textParagraphWith range: NSRange) -> NSTextParagraph? {
38-
guard let originalText = textContentStorage.textStorage?.attributedSubstring(from: range) as? NSMutableAttributedString else {
37+
guard let originalText = textContentStorage.textStorage?.attributedSubstring(from: range).mutableCopy() as? NSMutableAttributedString else {
3938
return nil
4039
}
4140

0 commit comments

Comments
 (0)