Skip to content

Commit 392e97f

Browse files
authored
Adds DefaultStringAttributes (#359)
1 parent 9ba5a1f commit 392e97f

4 files changed

Lines changed: 50 additions & 32 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Foundation
2+
import UIKit
3+
4+
struct DefaultStringAttributes {
5+
private let textColor: UIColor
6+
private let font: UIFont
7+
private let kern: CGFloat
8+
private let tabWidth: CGFloat
9+
private let lineHeightMultiplier: CGFloat
10+
11+
init(
12+
textColor: UIColor,
13+
font: UIFont,
14+
kern: CGFloat,
15+
tabWidth: CGFloat,
16+
lineHeightMultiplier: CGFloat = 1
17+
) {
18+
self.textColor = textColor
19+
self.font = font
20+
self.kern = kern
21+
self.tabWidth = tabWidth
22+
self.lineHeightMultiplier = lineHeightMultiplier
23+
}
24+
25+
func apply(to attributedString: NSMutableAttributedString) {
26+
let paragraphStyle = NSMutableParagraphStyle()
27+
paragraphStyle.tabStops = []
28+
paragraphStyle.defaultTabInterval = tabWidth
29+
paragraphStyle.lineHeightMultiple = lineHeightMultiplier
30+
let range = NSRange(location: 0, length: attributedString.length)
31+
let attributes: [NSAttributedString.Key: Any] = [
32+
.foregroundColor: textColor,
33+
.font: font,
34+
.kern: kern as NSNumber,
35+
.paragraphStyle: paragraphStyle
36+
]
37+
attributedString.beginEditing()
38+
attributedString.setAttributes(attributes, range: range)
39+
attributedString.endEditing()
40+
}
41+
}

Sources/Runestone/TextView/Core/TextInputView.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,9 +1623,7 @@ extension TextInputView: LineControllerStorageDelegate {
16231623
// MARK: - LineControllerDelegate
16241624
extension TextInputView: LineControllerDelegate {
16251625
func lineSyntaxHighlighter(for lineController: LineController) -> LineSyntaxHighlighter? {
1626-
let syntaxHighlighter = languageMode.createLineSyntaxHighlighter()
1627-
syntaxHighlighter.kern = kern
1628-
return syntaxHighlighter
1626+
languageMode.createLineSyntaxHighlighter()
16291627
}
16301628

16311629
func lineControllerDidInvalidateLineWidthDuringAsyncSyntaxHighlight(_ lineController: LineController) {

Sources/Runestone/TextView/LineController/LineController.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ final class LineController {
6464
var kern: CGFloat = 0 {
6565
didSet {
6666
if kern != oldValue {
67-
syntaxHighlighter?.kern = kern
67+
isDefaultAttributesInvalid = true
6868
}
6969
}
7070
}
@@ -240,25 +240,20 @@ private extension LineController {
240240
private func updateDefaultAttributesIfNecessary() {
241241
if isDefaultAttributesInvalid {
242242
if let input = createLineSyntaxHighlightInput() {
243-
syntaxHighlighter?.setDefaultAttributes(on: input.attributedString)
243+
let defaultStringAttributes = DefaultStringAttributes(
244+
textColor: theme.textColor,
245+
font: theme.font,
246+
kern: kern,
247+
tabWidth: tabWidth
248+
)
249+
defaultStringAttributes.apply(to: input.attributedString)
244250
}
245-
updateParagraphStyle()
246251
isDefaultAttributesInvalid = false
247252
isSyntaxHighlightingInvalid = true
248253
isTypesetterInvalid = true
249254
}
250255
}
251256

252-
private func updateParagraphStyle() {
253-
if let attributedString = attributedString {
254-
let paragraphStyle = NSMutableParagraphStyle()
255-
paragraphStyle.tabStops = []
256-
paragraphStyle.defaultTabInterval = tabWidth
257-
let range = NSRange(location: 0, length: attributedString.length)
258-
attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
259-
}
260-
}
261-
262257
private func updateTypesetterIfNecessary() {
263258
if isTypesetterInvalid {
264259
lineFragmentTree.reset(rootValue: 0, rootData: LineFragmentNodeData(lineFragment: nil))

Sources/Runestone/TextView/LineController/LineSyntaxHighlighter.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,8 @@ final class LineSyntaxHighlighterInput {
2323
protocol LineSyntaxHighlighter: AnyObject {
2424
typealias AsyncCallback = (Result<Void, Error>) -> Void
2525
var theme: Theme { get set }
26-
var kern: CGFloat { get set }
2726
var canHighlight: Bool { get }
28-
func setDefaultAttributes(on attributedString: NSMutableAttributedString)
2927
func syntaxHighlight(_ input: LineSyntaxHighlighterInput)
3028
func syntaxHighlight(_ input: LineSyntaxHighlighterInput, completion: @escaping AsyncCallback)
3129
func cancel()
3230
}
33-
34-
extension LineSyntaxHighlighter {
35-
func setDefaultAttributes(on attributedString: NSMutableAttributedString) {
36-
let entireRange = NSRange(location: 0, length: attributedString.length)
37-
let attributes: [NSAttributedString.Key: Any] = [
38-
.foregroundColor: theme.textColor,
39-
.font: theme.font,
40-
.kern: kern as NSNumber
41-
]
42-
attributedString.beginEditing()
43-
attributedString.setAttributes(attributes, range: entireRange)
44-
attributedString.endEditing()
45-
}
46-
}

0 commit comments

Comments
 (0)