Skip to content

Instantly share code, notes, and snippets.

@DavidPiper94
Last active March 4, 2021 05:53
Show Gist options
  • Select an option

  • Save DavidPiper94/32e57a9357e310a45eb440adb80219e2 to your computer and use it in GitHub Desktop.

Select an option

Save DavidPiper94/32e57a9357e310a45eb440adb80219e2 to your computer and use it in GitHub Desktop.
Example code for article about TagCloudView - Creation of Rows
private func setupRows() {
// 1: Loop over items.
for item in itemViews {
guard let currentRow = rows.last else { return }
// 2: Check whether to extend current row or start a new row.
let currentRowWidth = currentRow.arrangedSubviews
.map(\.frame.width)
.reduce(0, +)
// 3: Extend current row.
if frame.width > currentRowWidth + item.expectedWidth {
currentRow.addArrangedSubview(item)
} else {
// 4: Add empty view to align previous row to the left.
let fillerView = NSView()
currentRow.addArrangedSubview(fillerView)
// 5: Start next row.
let newRowStackView = emptyRowStackView
newRowStackView.addArrangedSubview(item)
rows.append(newRowStackView)
}
}
// 6: Add empty view to align last row to the left.
let fillerView = NSView()
rows.last?.addArrangedSubview(fillerView)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment