Skip to content

Commit 00a4241

Browse files
committed
Fix content view frame and origins for text selection and interaction
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.
1 parent 6ec6723 commit 00a4241

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

Sources/STTextViewUIKit/STTextView+UITextInput.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ extension STTextView: UITextInput {
226226
/* Geometry used to provide, for example, a correction rect. */
227227

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

232232
public func caretRect(for position: UITextPosition) -> CGRect {
@@ -311,7 +311,7 @@ extension STTextView: UITextInput {
311311
y: selectionFrame.origin.y,
312312
width: max(2, selectionFrame.width),
313313
height: typingLineHeight
314-
).moved(dx: -contentView.bounds.origin.x).pixelAligned
314+
).moved(dx: contentView.frame.origin.x).pixelAligned
315315
}
316316

317317
return .zero
@@ -341,7 +341,7 @@ extension STTextView: UITextInput {
341341
}
342342

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

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

361361
public func closestPosition(to point: CGPoint, within range: UITextRange) -> UITextPosition? {

Sources/STTextViewUIKit/STTextView.swift

+8-7
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,10 @@ import STTextViewCommon
637637
}
638638

639639
open override func sizeToFit() {
640-
contentView.bounds.origin.x = -(gutterView?.frame.width ?? 0)
641-
contentView.frame.size.width = max(textLayoutManager.usageBoundsForTextContainer.size.width, bounds.width - (gutterView?.frame.width ?? 0))
642-
contentView.frame.size.height = max(textLayoutManager.usageBoundsForTextContainer.size.height, bounds.height)
640+
let gutterWidth = gutterView?.frame.width ?? 0
641+
contentView.frame.origin.x = gutterWidth
642+
contentView.frame.size.width = max(textLayoutManager.usageBoundsForTextContainer.size.width, frame.width - gutterWidth)
643+
contentView.frame.size.height = max(textLayoutManager.usageBoundsForTextContainer.size.height, frame.height)
643644
contentSize = contentView.frame.size
644645

645646
super.sizeToFit()
@@ -882,7 +883,7 @@ import STTextViewCommon
882883
if let selectionFrame = textLayoutManager.textSegmentFrame(at: textLayoutManager.documentRange.location, type: .standard) {
883884
lineHighlightView.frame = CGRect(
884885
origin: CGPoint(
885-
x: contentView.frame.origin.x,
886+
x: 0,
886887
y: selectionFrame.origin.y
887888
),
888889
size: CGSize(
@@ -937,11 +938,11 @@ import STTextViewCommon
937938

938939
lineSelectionRectangle = CGRect(
939940
origin: CGPoint(
940-
x: contentView.frame.origin.x,
941+
x: 0,
941942
y: lineFragmentFrame.origin.y + textLineFragment.typographicBounds.minY
942943
),
943944
size: CGSize(
944-
width: contentView.frame.size.width,
945+
width: contentView.bounds.size.width,
945946
height: lineFragmentFrame.height
946947
)
947948
)
@@ -953,7 +954,7 @@ import STTextViewCommon
953954

954955
lineSelectionRectangle = CGRect(
955956
origin: CGPoint(
956-
x: contentView.bounds.minX,
957+
x: 0,
957958
y: lineFragmentFrame.origin.y + prevTextLineFragment.typographicBounds.maxY
958959
),
959960
size: CGSize(

0 commit comments

Comments
 (0)