Skip to content

Commit

Permalink
Fix rendering thematic break with centered or right aligned text (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guille Gonzalez authored Jul 22, 2022
1 parent e0eb1a9 commit 6a33c60
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/MarkdownUI/Rendering/AttributedStringRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ extension AttributedStringRenderer {

result.addAttribute(
.paragraphStyle,
value: paragraphStyle(state: state),
value: paragraphStyle(state: state, alignment: .natural),
range: NSRange(0..<result.length)
)

Expand Down Expand Up @@ -492,12 +492,12 @@ extension AttributedStringRenderer {
} ?? NSAttributedString()
}

private func paragraphStyle(state: State) -> NSParagraphStyle {
private func paragraphStyle(state: State, alignment: NSTextAlignment? = nil) -> NSParagraphStyle {
let pointSize = state.font.resolve(sizeCategory: environment.sizeCategory).pointSize
let result = NSMutableParagraphStyle()
result.setParagraphStyle(.default)
result.baseWritingDirection = environment.baseWritingDirection
result.alignment = environment.alignment
result.alignment = alignment ?? environment.alignment
result.lineSpacing = environment.lineSpacing
result.paragraphSpacing = round(pointSize * state.paragraphSpacing)
result.headIndent = round(pointSize * state.headIndent)
Expand Down
27 changes: 27 additions & 0 deletions Tests/MarkdownUITests/MarkdownTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,33 @@
assertSnapshot(matching: view, as: .image(layout: layout), named: platformName)
}

func testThematicBreakAndCenterAlignment() {
let view = Markdown(
#"""
# SwiftUI
Declare the user interface and behavior for your app
on every platform.
---
## Overview
SwiftUI provides views, controls, and layout structures
for declaring your app’s user interface.
---
― From Apple Developer Documentation
"""#
)
.multilineTextAlignment(.center)
.background(Color.orange)
.padding()

assertSnapshot(matching: view, as: .image(layout: layout), named: platformName)
}

func testInlines() {
let view = Markdown(
#"""
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6a33c60

Please sign in to comment.