Skip to content

Instantly share code, notes, and snippets.

@DavidPiper94
DavidPiper94 / Usage.swift
Created March 3, 2021 05:25
Example code for article about TagCloudView - Using a TagCloudView
// 1
let tagCloudView = TagCloudView()
let items = ["This", "is", "a", "test"]
.map(TagCloudItemViewModel.init(title:))
tagCloudView.addItems(items: items)
// 2
class Delegate: TagCloudViewDelegate {
func removeClicked(title: String) {
print("Clicked remove button on tag \(title)")
@DavidPiper94
DavidPiper94 / Resizing.swift
Last active March 4, 2021 05:18
Example code for article about TagCloudView - Reacting to Resizing
// 1
var timer: Timer?
override func viewWillStartLiveResize() {
super.viewWillStartLiveResize()
startTimer()
}
// 2
func startTimer() {
let repeatedTimer = Timer.scheduledTimer(
@DavidPiper94
DavidPiper94 / ShowRows.swift
Created March 3, 2021 05:25
Example code for article about TagCloudView - Showing Rows
private func showRows() {
for row in rows {
rowStackView.addArrangedSubview(row)
}
}
@DavidPiper94
DavidPiper94 / SetupRows.swift
Last active March 4, 2021 05:53
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, +)
@DavidPiper94
DavidPiper94 / ClearRows.swift
Created March 3, 2021 05:25
Example code for article about TagCloudView - Clearing previous Rows
private func clearRows() {
for row in rows {
rowStackView.removeArrangedSubview(row)
row.removeFromSuperview()
}
rows = [emptyRowStackView]
}
@DavidPiper94
DavidPiper94 / BuildRows.swift
Created March 3, 2021 05:25
Example code for article about TagCloudView - Building Rows
// 1
private var itemViews = [TagCloudItemView]()
private var rows = [NSStackView]()
private var rowStackView = NSStackView()
private var emptyRowStackView = NSStackView()
/* Configure and style these views... */
// 2
func buildRows() {
clearRows()
@DavidPiper94
DavidPiper94 / spm.swift
Last active April 9, 2020 05:28
Example code for article about SourceKitten - Using the swift package manager to include SourceKitten in your app.
dependencies: [
// ...
.package(url: "https://github.com/jpsim/SourceKitten", from: "0.27.0")
]
@DavidPiper94
DavidPiper94 / Usage.swift
Last active April 9, 2020 05:28
Example code for article about SourceKitten - Analyzing the source code
// 1
import SourceKittenFramework
// 2
let file = File(path: "/path/to/sourcefile.swift")!
// 3
let code = """
import UIKit
@DavidPiper94
DavidPiper94 / Parsing.swift
Last active April 9, 2020 05:28
Example code for article about SourceKitten - Parsing the result
// 1
func syntaxStructure(from file: File) -> SyntaxStructure? {
do {
// 2
let structure = try Structure(file: file)
let jsonData = structure.description.data(using: .utf8)!
// 3
return try JSONDecoder().decode(SyntaxStructure.self, from: jsonData)
@DavidPiper94
DavidPiper94 / SyntaxStructure.swift
Last active April 9, 2020 05:28
Example code for article about SourceKitten - SyntaxStructure
struct SyntaxStructure: Codable {
// 1
let accessibility: String?
let attribute: String?
let attributes: [SyntaxStructure]?
let bodylength: Int?
let bodyoffset: Int?
let diagnosticstage: String?
let elements: [SyntaxStructure]?