diff --git a/Proton/Sources/Swift/Attachment/Attachment.swift b/Proton/Sources/Swift/Attachment/Attachment.swift index 4b1052d0..24fa1ad8 100644 --- a/Proton/Sources/Swift/Attachment/Attachment.swift +++ b/Proton/Sources/Swift/Attachment/Attachment.swift @@ -359,12 +359,13 @@ open class Attachment: NSTextAttachment, BoundsObserving { // incorrect width if bounds are calculated directly on `attributedText` instead of using `textView`. let lineFragmentPadding = containerEditorView.richTextView.textContainer.lineFragmentPadding // Account for text leading and trailing margins within the textContainer + let horizontalTextInsets = (containerEditorView.textContainerInset.left + containerEditorView.textContainerInset.right) let adjustedContainerSize = CGSize( - width: containerEditorView.bounds.size.width - (lineFragmentPadding * 2) - indent, + width: containerEditorView.bounds.size.width - (lineFragmentPadding * 2) - indent - horizontalTextInsets, height: containerEditorView.bounds.size.height ) let adjustedLineFrag = CGRect( - x: lineFrag.origin.x, + x: lineFrag.origin.x + containerEditorView.textContainerInset.left, y: lineFrag.origin.y, width: min(lineFrag.size.width, adjustedContainerSize.width), height: lineFrag.height diff --git a/Proton/Sources/Swift/Core/RichTextView.swift b/Proton/Sources/Swift/Core/RichTextView.swift index c624a0ed..390380f0 100644 --- a/Proton/Sources/Swift/Core/RichTextView.swift +++ b/Proton/Sources/Swift/Core/RichTextView.swift @@ -309,6 +309,7 @@ class RichTextView: AutogrowingTextView { placeholderLabel.numberOfLines = 0 placeholderLabel.lineBreakMode = .byTruncatingTail + placeholderLabel.removeFromSuperview() addSubview(placeholderLabel) placeholderLabel.attributedText = placeholderText NSLayoutConstraint.activate([ diff --git a/Proton/Sources/Swift/Editor/EditorView.swift b/Proton/Sources/Swift/Editor/EditorView.swift index 1bc3ec06..c95a16c5 100644 --- a/Proton/Sources/Swift/Editor/EditorView.swift +++ b/Proton/Sources/Swift/Editor/EditorView.swift @@ -1197,7 +1197,9 @@ extension EditorView { size = contentSize } - frame = CGRect(origin: frame.origin, size: size) + var adjustedOrigin = frame.origin + adjustedOrigin.x += textContainerInset.left + frame = CGRect(origin: adjustedOrigin, size: size) if attachment.isRendered == false { attachment.render(in: self) @@ -1205,6 +1207,7 @@ extension EditorView { focusable.setFocus() } } + attachment.frame = frame } } diff --git a/Proton/Tests/Editor/EditorSnapshotTests.swift b/Proton/Tests/Editor/EditorSnapshotTests.swift index cfe1d2f6..1cbbd2a4 100644 --- a/Proton/Tests/Editor/EditorSnapshotTests.swift +++ b/Proton/Tests/Editor/EditorSnapshotTests.swift @@ -214,6 +214,33 @@ class EditorSnapshotTests: SnapshotTestCase { assertSnapshot(matching: viewController.view, as: .image, record: recordMode) } + func testRendersAttachmentWithTextContainerInset() { + let viewController = EditorTestViewController() + let editor = viewController.editor + let offsetProvider = MockAttachmentOffsetProvider() + offsetProvider.offset = CGPoint(x: 0, y: -4) + + editor.font = UIFont.systemFont(ofSize: 12) + editor.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20) + var panel = PanelView() + panel.backgroundColor = .cyan + panel.layer.borderWidth = 1.0 + panel.layer.cornerRadius = 4.0 + panel.layer.borderColor = UIColor.black.cgColor + + let attachment = Attachment(panel, size: .fullWidth) + panel.boundsObserver = attachment + panel.editor.font = editor.font + + panel.attributedText = NSAttributedString(string: "In full-width attachment") + + editor.replaceCharacters(in: .zero, with: "This text is in Editor") + editor.insertAttachment(in: editor.textEndRange, attachment: attachment) + + viewController.render() + assertSnapshot(matching: viewController.view, as: .image, record: recordMode) + } + func testDeletesAttachments() { let viewController = EditorTestViewController() let editor = viewController.editor diff --git a/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testRendersAttachmentWithContentInset.1.png b/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testRendersAttachmentWithContentInset.1.png new file mode 100644 index 00000000..7bca6db5 Binary files /dev/null and b/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testRendersAttachmentWithContentInset.1.png differ diff --git a/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testRendersAttachmentWithTextContainerInset.1.png b/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testRendersAttachmentWithTextContainerInset.1.png new file mode 100644 index 00000000..7bca6db5 Binary files /dev/null and b/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testRendersAttachmentWithTextContainerInset.1.png differ diff --git a/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testSelectionRects.1.png b/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testSelectionRects.1.png index bfbe6d13..ddc200fd 100644 Binary files a/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testSelectionRects.1.png and b/Proton/Tests/Editor/__Snapshots__/EditorSnapshotTests/testSelectionRects.1.png differ