Last active
March 4, 2021 05:53
-
-
Save DavidPiper94/32e57a9357e310a45eb440adb80219e2 to your computer and use it in GitHub Desktop.
Example code for article about TagCloudView - Creation of Rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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