-
Notifications
You must be signed in to change notification settings - Fork 288
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
TextKit 2 - Implement basic TK2 class and graceful fallback to TK1 #1690
Merged
charliescheer
merged 7 commits into
feature/text-kit-2
from
charlie/textkit-2-round2-mk1
Dec 26, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7e6befc
Setup using text kit 2 classes with SPTextView
charliescheer 807eed1
Format text with TextKit2 content storage methods
charliescheer 81268e5
Added SPFallbackText Container file
charliescheer 1332a10
Fixed issue where falling back to text kit 1 lost formatting
charliescheer 18ae0c2
Revert "Fixed issue where falling back to text kit 1 lost formatting"
charliescheer 4aecd53
Revert "Added SPFallbackText Container file"
charliescheer 0a48d63
Setup layoutmanager explicitly when setting up sptextview
charliescheer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// 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 16.0, *) { | ||
let textLayoutManager = NSTextLayoutManager() | ||
let contentStorage = NSTextContentStorage() | ||
contentStorage.delegate = self | ||
contentStorage.addTextLayoutManager(textLayoutManager) | ||
textLayoutManager.textContainer = container | ||
|
||
} else { | ||
let layoutManager = NSLayoutManager() | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
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 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick note:
layoutManager
may not be init'ed by the time it's called here