Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: TextKit 2 mk1 #1689

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Simplenote.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,9 @@
BAB6C04526BA4A04007495C4 /* String+Simplenote.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5476BC023D8E5D0000E7723 /* String+Simplenote.swift */; };
BAB6C04726BA4CAF007495C4 /* WidgetController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB6C04626BA4CAF007495C4 /* WidgetController.swift */; };
BAB898D32BEC404200E238B8 /* CreateNewNoteIntentHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB898D22BEC404200E238B8 /* CreateNewNoteIntentHandler.swift */; };
BABB22DF2D14DA6600FCF47D /* SPTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABB22DE2D14DA6300FCF47D /* SPTextView.swift */; };
BABB22E12D162E6D00FCF47D /* NSTextLayoutManager+Simplenote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABB22E02D162E6200FCF47D /* NSTextLayoutManager+Simplenote.swift */; };
BABB22E32D162E8400FCF47D /* NSTextLayoutFragment.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABB22E22D162E7D00FCF47D /* NSTextLayoutFragment.swift */; };
BABFFF2226CF9094003A4C25 /* WidgetDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABFFF2126CF9094003A4C25 /* WidgetDefaults.swift */; };
BABFFF2326CF9094003A4C25 /* WidgetDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABFFF2126CF9094003A4C25 /* WidgetDefaults.swift */; };
BABFFF2426CF9094003A4C25 /* WidgetDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = BABFFF2126CF9094003A4C25 /* WidgetDefaults.swift */; };
Expand Down Expand Up @@ -1242,6 +1245,9 @@
BAB576BD2670512C00B0C56F /* NoteWidget.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteWidget.swift; sourceTree = "<group>"; };
BAB6C04626BA4CAF007495C4 /* WidgetController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetController.swift; sourceTree = "<group>"; };
BAB898D22BEC404200E238B8 /* CreateNewNoteIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CreateNewNoteIntentHandler.swift; sourceTree = "<group>"; };
BABB22DE2D14DA6300FCF47D /* SPTextView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SPTextView.swift; sourceTree = "<group>"; };
BABB22E02D162E6200FCF47D /* NSTextLayoutManager+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSTextLayoutManager+Simplenote.swift"; sourceTree = "<group>"; };
BABB22E22D162E7D00FCF47D /* NSTextLayoutFragment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSTextLayoutFragment.swift; sourceTree = "<group>"; };
BABFFF2126CF9094003A4C25 /* WidgetDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetDefaults.swift; sourceTree = "<group>"; };
BAD0F1EC2BED49C200E73E45 /* FindNoteIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FindNoteIntentHandler.swift; sourceTree = "<group>"; };
BAE08625261282D1009D40CD /* Note+Publish.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Note+Publish.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1872,10 +1878,13 @@
children = (
B5E3F3972539F1E900271AEA /* SPTextView.h */,
B5E3F3962539F1E900271AEA /* SPTextView.m */,
BABB22DE2D14DA6300FCF47D /* SPTextView.swift */,
E21F57B717C1244E001F02D3 /* SPEditorTextView.h */,
E21F57B817C1244E001F02D3 /* SPEditorTextView.m */,
A6BBDA45255034E6005C8343 /* SPEditorTextView+Simplenote.swift */,
B52E2B142537480A0074509A /* SPEditorTapRecognizerDelegate.swift */,
BABB22E02D162E6200FCF47D /* NSTextLayoutManager+Simplenote.swift */,
BABB22E22D162E7D00FCF47D /* NSTextLayoutFragment.swift */,
);
name = Editor;
sourceTree = "<group>";
Expand Down Expand Up @@ -3468,6 +3477,7 @@
A60DF30825A44F0F00FDADF3 /* PinLockRemoveController.swift in Sources */,
A694ABAB25D1549D00CC3A2D /* FileStorage.swift in Sources */,
B513FB2422EF6A4B00B178AC /* SPUserInterface.swift in Sources */,
BABB22E32D162E8400FCF47D /* NSTextLayoutFragment.swift in Sources */,
B5DF734022A54EE800602CE7 /* SPDefaultTableViewCell.swift in Sources */,
B5796549250684EC00DFD6F7 /* EditorFactory.swift in Sources */,
A6C2721825AF0C1E00593731 /* TagListViewCell.swift in Sources */,
Expand Down Expand Up @@ -3539,6 +3549,7 @@
A6CDF900256B9CB900CF2F27 /* ViewSpinner.swift in Sources */,
375D24B721E01131007AB25A /* stack.c in Sources */,
373AD30821C4739500A4EA89 /* NSMutableAttributedString+Styling.m in Sources */,
BABB22E12D162E6D00FCF47D /* NSTextLayoutManager+Simplenote.swift in Sources */,
B524AE112352CC7900EA11D4 /* UIScreen+Simplenote.swift in Sources */,
A68A4345256FEE3000D1CA5D /* SPSettingsViewController+Extensions.swift in Sources */,
B543C7E523CF775C00003A80 /* NSSortDescriptor+Simplenote.swift in Sources */,
Expand All @@ -3562,6 +3573,7 @@
B53C5A5A230330CD00DA2143 /* SPNoteListViewController+Extensions.swift in Sources */,
A6C0DFB525C1581D00B9BE39 /* UIScrollView+Simplenote.swift in Sources */,
375D24B621E01131007AB25A /* html_blocks.c in Sources */,
BABB22DF2D14DA6600FCF47D /* SPTextView.swift in Sources */,
46A3C98217DFA81A002865AE /* NSString+Attributed.m in Sources */,
375D24BA21E01131007AB25A /* document.c in Sources */,
BA6DA19126DB5F1B000464C8 /* URLComponents.swift in Sources */,
Expand Down
9 changes: 2 additions & 7 deletions Simplenote/Classes/SPTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ @implementation SPTextView
- (instancetype)init {

SPInteractiveTextStorage *textStorage = [[SPInteractiveTextStorage alloc] init];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];

NSTextContainer *container = [[NSTextContainer alloc] initWithSize:CGSizeMake(0, CGFLOAT_MAX)];
container.widthTracksTextView = YES;
container.heightTracksTextView = YES;
[layoutManager addTextContainer:container];
[textStorage addLayoutManager:layoutManager];

NSTextContainer *container = [self setupTextContainerWith:textStorage];

self = [super initWithFrame:CGRectZero textContainer:container];
if (self) {
self.interactiveTextStorage = textStorage;
Expand Down
14 changes: 11 additions & 3 deletions Simplenote/Classes/UIGestureRecognizer+Simplenote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@ extension UIGestureRecognizer {
locationInContainer.x -= textView.textContainerInset.left
locationInContainer.y -= textView.textContainerInset.top

return textView.layoutManager.characterIndex(for: locationInContainer,
in: textView.textContainer,
fractionOfDistanceBetweenInsertionPoints: nil)
guard #available(iOS 17.0, *),
let textLayoutManager = textView.textLayoutManager else {
// TextKit 1 fallback
return textView.layoutManager.characterIndex(
for: locationInContainer,
in: textView.textContainer,
fractionOfDistanceBetweenInsertionPoints: nil)
}

// TextKit 2
return textLayoutManager.characterIndex(for: locationInContainer) ?? .zero
}
}
17 changes: 17 additions & 0 deletions Simplenote/NSTextLayoutFragment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSTextLayoutFragment.swift
// Simplenote
//
// Created by Charlie Scheer on 12/20/24.
// Copyright © 2024 Automattic. All rights reserved.
//

extension NSTextLayoutFragment {
@available(iOS 17.0, *)
func characterIndex(for location: CGPoint) -> Int? {
guard let lineFragment = textLineFragment(forVerticalOffset: location.y, requiresExactMatch: true) else {
return nil
}
return lineFragment.characterIndex(for: location)
}
}
17 changes: 17 additions & 0 deletions Simplenote/NSTextLayoutManager+Simplenote.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NSTextLayoutManager+Simplenote.swift
// Simplenote
//
// Created by Charlie Scheer on 12/20/24.
// Copyright © 2024 Automattic. All rights reserved.
//

extension NSTextLayoutManager {
@available(iOS 17.0, *)
func characterIndex(for location: CGPoint) -> Int? {
guard let lineFragment = textLayoutFragment(for: location) else {
return nil
}
return lineFragment.characterIndex(for: location)
}
}
94 changes: 94 additions & 0 deletions Simplenote/SPTextView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//
// SPTextView.swift
// Simplenote
//
// Created by Charlie Scheer on 12/19/24.
// Copyright © 2024 Automattic. All rights reserved.
//

extension SPTextView {
@objc
func setupTextContainer(with textStorage: SPInteractiveTextStorage) -> NSTextContainer {
let container = NSTextContainer(size: .zero)
container.widthTracksTextView = true
container.heightTracksTextView = true


if #available(iOS 17.0, *) {
let textLayoutManager = NSTextLayoutManager()
let contentStorage = NSTextContentStorage()
contentStorage.delegate = self
contentStorage.addTextLayoutManager(textLayoutManager)
textLayoutManager.textContainer = container

} else {
layoutManager.addTextContainer(container)
textStorage.addLayoutManager(layoutManager)
}

return container
}
}

// MARK: NSTextContentStorageDelegate
//
extension SPTextView: NSTextContentStorageDelegate {
public func textContentStorage(_ textContentStorage: NSTextContentStorage, textParagraphWith range: NSRange) -> NSTextParagraph? {
guard let originalText = textContentStorage.textStorage?.attributedSubstring(from: range) as? NSMutableAttributedString else {
return nil
}

let style = textInRangeIsHeader(range) ? headlineStyle : defaultStyle
originalText.addAttributes(style, range: originalText.fullRange)

return NSTextParagraph(attributedString: originalText)
}

func textInRangeIsHeader(_ range: NSRange) -> Bool {
range.location == .zero
}

// MARK: Styles
//
var headlineFont: UIFont {
UIFont.preferredFont(for: .title1, weight: .bold)
}

var defaultFont: UIFont {
UIFont.preferredFont(forTextStyle: .body)
}

var defaultTextColor: UIColor {
UIColor.simplenoteNoteHeadlineColor
}

var lineSpacing: CGFloat {
defaultFont.lineHeight * Metrics.lineSpacingMultipler
}

var defaultStyle: [NSAttributedString.Key: Any] {
[
.font: defaultFont,
.foregroundColor: defaultTextColor,
.paragraphStyle: NSMutableParagraphStyle(lineSpacing: lineSpacing)
]
}

var headlineStyle: [NSAttributedString.Key: Any] {
[
.font: headlineFont,
.foregroundColor: defaultTextColor,
]
}
}

// MARK: - Metrics
//
private enum Metrics {
static let lineSpacingMultiplerPad: CGFloat = 0.40
static let lineSpacingMultiplerPhone: CGFloat = 0.20

static var lineSpacingMultipler: CGFloat {
UIDevice.isPad ? lineSpacingMultiplerPad : lineSpacingMultiplerPhone
}
}