Skip to content

Commit

Permalink
Format text with TextKit2 content storage methods
Browse files Browse the repository at this point in the history
  • Loading branch information
charliescheer committed Dec 20, 2024
1 parent 7e6befc commit 807eed1
Showing 1 changed file with 64 additions and 11 deletions.
75 changes: 64 additions & 11 deletions Simplenote/SPTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@
//

extension SPTextView {
/*
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];
*/

@objc
func setupTextContainer(with textStorage: SPInteractiveTextStorage) -> NSTextContainer {
let container = NSTextContainer(size: .zero)
Expand All @@ -28,6 +17,7 @@ extension SPTextView {
if #available(iOS 16.0, *) {
let textLayoutManager = NSTextLayoutManager()
let contentStorage = NSTextContentStorage()
contentStorage.delegate = self
contentStorage.addTextLayoutManager(textLayoutManager)
textLayoutManager.textContainer = container

Expand All @@ -39,3 +29,66 @@ extension SPTextView {
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
}
}

0 comments on commit 807eed1

Please sign in to comment.