Skip to content

Commit 30a4699

Browse files
committed
Update EmphasizeAPI Colors On Draw
1 parent 4cc8772 commit 30a4699

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

Sources/CodeEditTextView/EmphasizeAPI/EmphasizeAPI.swift

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class EmphasizeAPI {
2525
}
2626

2727
// MARK: - Structs
28+
2829
public struct EmphasizedRange {
2930
public var range: NSRange
3031
var layer: CAShapeLayer
@@ -71,7 +72,7 @@ public class EmphasizeAPI {
7172

7273
let layer = createEmphasizeLayer(shapePath: shapePath, active: active)
7374
textView?.layer?.insertSublayer(layer, at: 1)
74-
75+
7576
// Create and add text layer
7677
if let textLayer = createTextLayer(for: range, active: active) {
7778
textView?.layer?.addSublayer(textLayer)
@@ -140,6 +141,21 @@ public class EmphasizeAPI {
140141
textView?.needsDisplay = true
141142
}
142143

144+
package func updateLayerBackgrounds() {
145+
emphasizedRanges.enumerated().forEach { (idx, range) in
146+
let isActive = emphasizedRangeIndex == idx
147+
range.layer.fillColor = (isActive ? activeColor : inactiveColor).cgColor
148+
149+
guard let attributedString = range.textLayer?.string as? NSAttributedString else { return }
150+
let mutableString = NSMutableAttributedString(attributedString: attributedString)
151+
mutableString.addAttributes(
152+
[.foregroundColor: isActive ? NSColor.black : getInactiveTextColor()],
153+
range: NSRange(location: 0, length: range.range.length)
154+
)
155+
range.textLayer?.string = mutableString
156+
}
157+
}
158+
143159
// MARK: - Private Methods
144160

145161
private func createEmphasizeLayer(shapePath: NSBezierPath, active: Bool) -> CAShapeLayer {
@@ -226,7 +242,9 @@ public class EmphasizeAPI {
226242
guard let textView = textView,
227243
let layoutManager = textView.layoutManager,
228244
let shapePath = layoutManager.roundedPathForRange(range),
229-
let originalString = textView.textStorage?.attributedSubstring(from: range) else { return nil }
245+
let originalString = textView.textStorage?.attributedSubstring(from: range) else {
246+
return nil
247+
}
230248

231249
var bounds = shapePath.bounds
232250
bounds.origin.y += 1 // Move down by 1 pixel
@@ -252,16 +270,20 @@ public class EmphasizeAPI {
252270

253271
private func updateTextLayer(_ textLayer: CATextLayer, with originalString: NSAttributedString, active: Bool) {
254272
let text = NSMutableAttributedString(attributedString: originalString)
255-
text.addAttribute(.foregroundColor,
256-
value: active ? NSColor.black : getInactiveTextColor(),
257-
range: NSRange(location: 0, length: text.length))
273+
text.addAttribute(
274+
.foregroundColor,
275+
value: active ? NSColor.black : getInactiveTextColor(),
276+
range: NSRange(location: 0, length: text.length)
277+
)
258278
textLayer.string = text
259279
}
260280

261281
private func setTextColorForRange(_ range: NSRange, active: Bool) {
262282
guard let index = emphasizedRanges.firstIndex(where: { $0.range == range }),
263283
let textLayer = emphasizedRanges[index].textLayer,
264-
let originalString = textView?.textStorage?.attributedSubstring(from: range) else { return }
284+
let originalString = textView?.textStorage?.attributedSubstring(from: range) else {
285+
return
286+
}
265287

266288
updateTextLayer(textLayer, with: originalString, active: active)
267289
}

Sources/CodeEditTextView/TextView/TextView+Layout.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extension TextView {
2222
if isSelectable {
2323
selectionManager.drawSelections(in: dirtyRect)
2424
}
25+
emphasizeAPI?.updateLayerBackgrounds()
2526
}
2627

2728
override open var isFlipped: Bool {

0 commit comments

Comments
 (0)