Skip to content

Instantly share code, notes, and snippets.

View zenangst's full-sized avatar
🥪
Reminiscing about vacation

Christoffer Winterkvist zenangst

🥪
Reminiscing about vacation
View GitHub Profile
@zenangst
zenangst / Example3.swift
Created February 16, 2018 11:01
How to write an NSViewController without interface builder - Example 3
import Cocoa
class MyViewController: NSViewController {
override func loadView() {
self.view = NSView()
}
}
let frame = CGRect(origin: .zero, size: CGSize(width: 100, height: 100))
let viewController = MyViewController()
@zenangst
zenangst / Example1.swift
Created February 18, 2018 11:09
How to make a label in macOS - Example 1
import UIKit
let label = UILabel()
label.frame = CGRect(origin: .zero, size: CGSize(width: 100, height: 44))
label.text = "My awesome label"
label.backgroundColor = .white
label.sizeToFit()
@zenangst
zenangst / Example2.swift
Created February 21, 2018 06:19
How to make a label in macOS - Example 2
import Cocoa
let label = NSTextField()
label.frame = CGRect(origin: .zero, size: CGSize(width: 100, height: 44))
label.stringValue = "My awesome label"
label.backgroundColor = .white
label.isBezeled = false
label.isEditable = false
label.sizeToFit()
@zenangst
zenangst / Example1.swift
Created February 21, 2018 17:45
How frames work on macOS - Example 1
import Cocoa
class ContainerView: ColorView {}
class ColorView: NSView {
required init(frame: CGRect, color: NSColor) {
super.init(frame: frame)
wantsLayer = true
layer?.backgroundColor = color.cgColor
}
@zenangst
zenangst / Example2.swift
Created February 21, 2018 17:46
How frames work on macOS - Example 2
import Cocoa
class ContainerView: ColorView {
override var isFlipped: Bool { return true }
}
class ColorView: NSView {
required init(frame: CGRect, color: NSColor) {
super.init(frame: frame)
wantsLayer = true
@zenangst
zenangst / Example3.swift
Created February 21, 2018 17:59
How frames work on macOS - Example 3
import Cocoa
class ContainerView: ColorView {
override var isFlipped: Bool { return true }
}
class ColorView: NSView {
required init(frame: CGRect, color: NSColor) {
super.init(frame: frame)
wantsLayer = true
@zenangst
zenangst / Example1.swift
Created February 26, 2018 19:58
How scroll views work on macOS - Example 1
import Cocoa
import PlaygroundSupport
let subviewFrame = CGRect(origin: .zero,
size: CGSize(width: 320, height: 640 * 4))
let gradient = CAGradientLayer()
gradient.colors = [
NSColor.blue.withAlphaComponent(0.2).cgColor,
NSColor.blue.withAlphaComponent(0.4).cgColor
]
@zenangst
zenangst / Example2.swift
Created February 26, 2018 20:06
How scroll views work on macOS - Example 2
import Cocoa
import PlaygroundSupport
class View: NSView {
override var isFlipped: Bool { return true }
}
let subviewFrame = CGRect(origin: .zero,
size: CGSize(width: 320, height: 640 * 4))
let gradient = CAGradientLayer()
@zenangst
zenangst / Example1.swift
Last active May 24, 2018 05:28
Converting between view coordinate systems - Example 1
import UIKit
let frame = CGRect(origin: .zero,
size: .init(width: 150, height: 150))
let window = UIWindow(frame: frame)
let rootView = UIView(frame: window.bounds)
rootView.backgroundColor = .white
window.addSubview(rootView)
let view = UIView(frame: CGRect(origin: .init(x: 25, y: 25),
@zenangst
zenangst / Example2.swift
Last active May 24, 2018 05:25
Converting between view coordinate systems - Example 2
import Cocoa
import PlaygroundSupport
let frame = CGRect(origin: .zero,
size: .init(width: 150, height: 150))
let window = NSView(frame: frame)
window.wantsLayer = true
window.layer?.backgroundColor = NSColor.white.cgColor
let view = NSView(frame: CGRect(origin: .init(x: 25, y: 25),