Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds custom spacing to lists #352

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions Shared/Examples/ListExampleFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ class ListExampleFactory: ExampleFactory {
document.add(list: featureList)

document.add(space: 20)

// Simple bullet point list with custom spacing
let customSpacingList = PDFList(indentations: [
(pre: 10.0, past: 20.0),
(pre: 20.0, past: 20.0),
(pre: 40.0, past: 20.0)
])

//A custom spacing can be setup, this property is optional
customSpacingList.spacing = 16

// By adding the item first to a list item with the dot symbol, all of them will inherit it
customSpacingList
.addItem(PDFListItem(symbol: .dot)
.addItems(items.map({ item in
PDFListItem(content: item)
})))
document.add(list: customSpacingList)

document.add(space: 20)


// Numbered list with unusual indentation
let weirdIndentationList = PDFList(indentations: [
Expand Down
1 change: 1 addition & 0 deletions Source/API/List/PDFList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import AppKit
*/
public class PDFList: PDFDocumentObject {

public var spacing: CGFloat?
/**
TODO: Documentation
*/
Expand Down
13 changes: 12 additions & 1 deletion Source/Internal/List/PDFListObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,22 @@
result += try createTextItem(generator: generator, container: container, text: item.text)

generator.layout.indentation.setLeft(indentation: originalLeftIndent, in: container)
handleCustomInterItemSpacing(item: item, generator: generator, container: container)
}

return result
}


private func handleCustomInterItemSpacing(item: (level: Int, text: String, symbol: PDFListItemSymbol), generator: PDFGenerator, container: PDFContainer) {
guard let interItemSpacing = list.spacing,
let lastItem = list.flatted().last,
item != lastItem else {
//No need to add a space if it's the last item or no spacing has been defined
return
}
generator.layout.heights.add(interItemSpacing, to: container)
}

Check warning on line 59 in Source/Internal/List/PDFListObject.swift

View check run for this annotation

Codecov / codecov/patch

Source/Internal/List/PDFListObject.swift#L58-L59

Added lines #L58 - L59 were not covered by tests

/**
TODO: Documentation
*/
Expand Down