Skip to content

Commit

Permalink
Fix content view frame and origins for text selection and interaction
Browse files Browse the repository at this point in the history
The content view frame origin is now set to the width of the gutter view,
instead of the negative width. This fixes issues where the origins and frames
for text selection rectangles, closest position calculations, and other text
interactions were incorrectly offset.

When sizing to fit, the content view frame is now set based on the text view's
frame width/height instead of bounds, and the content size is set to match.

The line highlight and selection rectangle calculations now use a fixed origin
of 0 for the x coordinate, instead of the content view's frame origin x. The
line selection rectangle width uses the content view's bounds width.
  • Loading branch information
krzyzanowskim committed Dec 15, 2024
1 parent 6ec6723 commit 00a4241
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Sources/STTextViewUIKit/STTextView+UITextInput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ extension STTextView: UITextInput {
/* Geometry used to provide, for example, a correction rect. */

public func firstRect(for range: UITextRange) -> CGRect {
textLayoutManager.textSegmentFrame(in: range.nsTextRange, type: .standard)?.moved(dx: -contentView.bounds.origin.x) ?? .zero
textLayoutManager.textSegmentFrame(in: range.nsTextRange, type: .standard)?.moved(dx: contentView.frame.origin.x) ?? .zero
}

public func caretRect(for position: UITextPosition) -> CGRect {
Expand Down Expand Up @@ -311,7 +311,7 @@ extension STTextView: UITextInput {
y: selectionFrame.origin.y,
width: max(2, selectionFrame.width),
height: typingLineHeight
).moved(dx: -contentView.bounds.origin.x).pixelAligned
).moved(dx: contentView.frame.origin.x).pixelAligned
}

return .zero
Expand Down Expand Up @@ -341,7 +341,7 @@ extension STTextView: UITextInput {
}

result.append(STTextSelectionRect(
rect: textSegmentFrame.moved(dx: -contentView.bounds.origin.x),
rect: textSegmentFrame.moved(dx: contentView.frame.origin.x),
writingDirection: baseWritingDirection(for: range.start, in: .forward),
containsStart: containsStart,
containsEnd: containsEnd,
Expand All @@ -355,7 +355,7 @@ extension STTextView: UITextInput {
/* Hit testing. */

public func closestPosition(to point: CGPoint) -> UITextPosition? {
textLayoutManager.location(interactingAt: point.moved(dx: contentView.bounds.origin.x), inContainerAt: textLayoutManager.documentRange.location)?.uiTextPosition
textLayoutManager.location(interactingAt: point.moved(dx: -contentView.frame.origin.x), inContainerAt: textLayoutManager.documentRange.location)?.uiTextPosition
}

public func closestPosition(to point: CGPoint, within range: UITextRange) -> UITextPosition? {
Expand Down
15 changes: 8 additions & 7 deletions Sources/STTextViewUIKit/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,10 @@ import STTextViewCommon
}

open override func sizeToFit() {
contentView.bounds.origin.x = -(gutterView?.frame.width ?? 0)
contentView.frame.size.width = max(textLayoutManager.usageBoundsForTextContainer.size.width, bounds.width - (gutterView?.frame.width ?? 0))
contentView.frame.size.height = max(textLayoutManager.usageBoundsForTextContainer.size.height, bounds.height)
let gutterWidth = gutterView?.frame.width ?? 0
contentView.frame.origin.x = gutterWidth
contentView.frame.size.width = max(textLayoutManager.usageBoundsForTextContainer.size.width, frame.width - gutterWidth)
contentView.frame.size.height = max(textLayoutManager.usageBoundsForTextContainer.size.height, frame.height)
contentSize = contentView.frame.size

super.sizeToFit()
Expand Down Expand Up @@ -882,7 +883,7 @@ import STTextViewCommon
if let selectionFrame = textLayoutManager.textSegmentFrame(at: textLayoutManager.documentRange.location, type: .standard) {
lineHighlightView.frame = CGRect(
origin: CGPoint(
x: contentView.frame.origin.x,
x: 0,
y: selectionFrame.origin.y
),
size: CGSize(
Expand Down Expand Up @@ -937,11 +938,11 @@ import STTextViewCommon

lineSelectionRectangle = CGRect(
origin: CGPoint(
x: contentView.frame.origin.x,
x: 0,
y: lineFragmentFrame.origin.y + textLineFragment.typographicBounds.minY
),
size: CGSize(
width: contentView.frame.size.width,
width: contentView.bounds.size.width,
height: lineFragmentFrame.height
)
)
Expand All @@ -953,7 +954,7 @@ import STTextViewCommon

lineSelectionRectangle = CGRect(
origin: CGPoint(
x: contentView.bounds.minX,
x: 0,
y: lineFragmentFrame.origin.y + prevTextLineFragment.typographicBounds.maxY
),
size: CGSize(
Expand Down

0 comments on commit 00a4241

Please sign in to comment.