|
| 1 | +// |
| 2 | +// SPTextView.swift |
| 3 | +// Simplenote |
| 4 | +// |
| 5 | +// Created by Charlie Scheer on 12/19/24. |
| 6 | +// Copyright © 2024 Automattic. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +extension SPTextView { |
| 10 | + @objc |
| 11 | + func setupTextContainer(with textStorage: SPInteractiveTextStorage) -> NSTextContainer { |
| 12 | + let container = NSTextContainer(size: .zero) |
| 13 | + container.widthTracksTextView = true |
| 14 | + container.heightTracksTextView = true |
| 15 | + |
| 16 | + |
| 17 | + if #available(iOS 16.0, *) { |
| 18 | + let textLayoutManager = NSTextLayoutManager() |
| 19 | + let contentStorage = NSTextContentStorage() |
| 20 | + contentStorage.delegate = self |
| 21 | + contentStorage.addTextLayoutManager(textLayoutManager) |
| 22 | + textLayoutManager.textContainer = container |
| 23 | + |
| 24 | + } else { |
| 25 | + let layoutManager = NSLayoutManager() |
| 26 | + layoutManager.addTextContainer(container) |
| 27 | + textStorage.addLayoutManager(layoutManager) |
| 28 | + } |
| 29 | + |
| 30 | + return container |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// MARK: NSTextContentStorageDelegate |
| 35 | +// |
| 36 | +extension SPTextView: NSTextContentStorageDelegate { |
| 37 | + public func textContentStorage(_ textContentStorage: NSTextContentStorage, textParagraphWith range: NSRange) -> NSTextParagraph? { |
| 38 | + guard let originalText = textContentStorage.textStorage?.attributedSubstring(from: range) as? NSMutableAttributedString else { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + let style = textInRangeIsHeader(range) ? headlineStyle : defaultStyle |
| 43 | + originalText.addAttributes(style, range: originalText.fullRange) |
| 44 | + |
| 45 | + return NSTextParagraph(attributedString: originalText) |
| 46 | + } |
| 47 | + |
| 48 | + func textInRangeIsHeader(_ range: NSRange) -> Bool { |
| 49 | + range.location == .zero |
| 50 | + } |
| 51 | + |
| 52 | + // MARK: Styles |
| 53 | + // |
| 54 | + var headlineFont: UIFont { |
| 55 | + UIFont.preferredFont(for: .title1, weight: .bold) |
| 56 | + } |
| 57 | + |
| 58 | + var defaultFont: UIFont { |
| 59 | + UIFont.preferredFont(forTextStyle: .body) |
| 60 | + } |
| 61 | + |
| 62 | + var defaultTextColor: UIColor { |
| 63 | + UIColor.simplenoteNoteHeadlineColor |
| 64 | + } |
| 65 | + |
| 66 | + var lineSpacing: CGFloat { |
| 67 | + defaultFont.lineHeight * Metrics.lineSpacingMultipler |
| 68 | + } |
| 69 | + |
| 70 | + var defaultStyle: [NSAttributedString.Key: Any] { |
| 71 | + [ |
| 72 | + .font: defaultFont, |
| 73 | + .foregroundColor: defaultTextColor, |
| 74 | + .paragraphStyle: NSMutableParagraphStyle(lineSpacing: lineSpacing) |
| 75 | + ] |
| 76 | + } |
| 77 | + |
| 78 | + var headlineStyle: [NSAttributedString.Key: Any] { |
| 79 | + [ |
| 80 | + .font: headlineFont, |
| 81 | + .foregroundColor: defaultTextColor, |
| 82 | + ] |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// MARK: - Metrics |
| 87 | +// |
| 88 | +private enum Metrics { |
| 89 | + static let lineSpacingMultiplerPad: CGFloat = 0.40 |
| 90 | + static let lineSpacingMultiplerPhone: CGFloat = 0.20 |
| 91 | + |
| 92 | + static var lineSpacingMultipler: CGFloat { |
| 93 | + UIDevice.isPad ? lineSpacingMultiplerPad : lineSpacingMultiplerPhone |
| 94 | + } |
| 95 | +} |
0 commit comments