Skip to content

Commit

Permalink
List highlight the biggest frame rect
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzanowskim committed Dec 10, 2023
1 parent 2682fd1 commit fee9726
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions Sources/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,14 @@ import AVFoundation
return
}

// build the rectangle out of fragments rectangles
var combinedFragmentsRect: CGRect?

textLayoutManager.enumerateTextLayoutFragments(in: viewportRange) { layoutFragment in
let contentRangeInElement = (layoutFragment.textElement as? NSTextParagraph)?.paragraphContentRange ?? layoutFragment.rangeInElement
for lineFragment in layoutFragment.textLineFragments {

let isLineSelected: Bool = {
func isLineSelected() -> Bool {
textLayoutManager.textSelections.flatMap(\.textRanges).reduce(true) { partialResult, selectionTextRange in
var result = true
if lineFragment.isExtraLineFragment {
Expand All @@ -854,28 +857,37 @@ import AVFoundation
}
return partialResult && result
}
}()
}

if isLineSelected {
if isLineSelected() {
var lineFragmentFrame = layoutFragment.layoutFragmentFrame
lineFragmentFrame.size.height = lineFragment.typographicBounds.height

drawHighlight(
in: CGRect(
origin: CGPoint(
x: bounds.minX,
y: lineFragmentFrame.origin.y + lineFragment.typographicBounds.origin.y
),
size: CGSize(
width: max(scrollView?.contentSize.width ?? 0, bounds.width),
height: lineFragmentFrame.size.height
)

let r = CGRect(
origin: CGPoint(
x: bounds.minX,
y: lineFragmentFrame.origin.y + lineFragment.typographicBounds.minY
),
size: CGSize(
width: max(scrollView?.contentSize.width ?? 0, bounds.width),
height: lineFragmentFrame.height
)
)

if let rect = combinedFragmentsRect {
combinedFragmentsRect = rect.union(r)
} else {
combinedFragmentsRect = r
}
}
}
return true
}

if let combinedFragmentsRect {
drawHighlight(in: combinedFragmentsRect)
}
}

internal func setString(_ string: Any?) {
Expand Down

0 comments on commit fee9726

Please sign in to comment.