Skip to content

Commit 6a33c60

Browse files
author
Guille Gonzalez
authored
Fix rendering thematic break with centered or right aligned text (#114)
1 parent e0eb1a9 commit 6a33c60

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

Sources/MarkdownUI/Rendering/AttributedStringRenderer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ extension AttributedStringRenderer {
353353

354354
result.addAttribute(
355355
.paragraphStyle,
356-
value: paragraphStyle(state: state),
356+
value: paragraphStyle(state: state, alignment: .natural),
357357
range: NSRange(0..<result.length)
358358
)
359359

@@ -492,12 +492,12 @@ extension AttributedStringRenderer {
492492
} ?? NSAttributedString()
493493
}
494494

495-
private func paragraphStyle(state: State) -> NSParagraphStyle {
495+
private func paragraphStyle(state: State, alignment: NSTextAlignment? = nil) -> NSParagraphStyle {
496496
let pointSize = state.font.resolve(sizeCategory: environment.sizeCategory).pointSize
497497
let result = NSMutableParagraphStyle()
498498
result.setParagraphStyle(.default)
499499
result.baseWritingDirection = environment.baseWritingDirection
500-
result.alignment = environment.alignment
500+
result.alignment = alignment ?? environment.alignment
501501
result.lineSpacing = environment.lineSpacing
502502
result.paragraphSpacing = round(pointSize * state.paragraphSpacing)
503503
result.headIndent = round(pointSize * state.headIndent)

Tests/MarkdownUITests/MarkdownTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,33 @@
397397
assertSnapshot(matching: view, as: .image(layout: layout), named: platformName)
398398
}
399399

400+
func testThematicBreakAndCenterAlignment() {
401+
let view = Markdown(
402+
#"""
403+
# SwiftUI
404+
405+
Declare the user interface and behavior for your app
406+
on every platform.
407+
408+
---
409+
410+
## Overview
411+
412+
SwiftUI provides views, controls, and layout structures
413+
for declaring your app’s user interface.
414+
415+
---
416+
417+
― From Apple Developer Documentation
418+
"""#
419+
)
420+
.multilineTextAlignment(.center)
421+
.background(Color.orange)
422+
.padding()
423+
424+
assertSnapshot(matching: view, as: .image(layout: layout), named: platformName)
425+
}
426+
400427
func testInlines() {
401428
let view = Markdown(
402429
#"""
68.1 KB
Loading
78.4 KB
Loading

0 commit comments

Comments
 (0)