Skip to content

Instantly share code, notes, and snippets.

@bhrott
bhrott / notificationService.swift
Created March 31, 2017 16:39
Swift :: Service for Notifications
import Foundation
class NotificationService {
static private var _instance: NotificationService! = nil
static var sharedInstance: NotificationService {
get {
if NotificationService._instance == nil {
NotificationService._instance = NotificationService()
@Pretz
Pretz / uiviewcontroller-playground.swift
Created December 8, 2015 23:37
UIViewController Playground: because getting a UIViewController/UINavigationController to display properly in a playground is a little non-obvious
import UIKit
let vc = UIViewController()
vc.view.backgroundColor = .whiteColor()
vc.navigationItem.title = "This is a view controller"
let searchController = UISearchController(searchResultsController: nil)
//searchController.searchBar.barTintColor = UIColor(red:0.16, green:0.45, blue:0.72, alpha:1)
@Blackjacx
Blackjacx / NibLoadableView.swift
Created March 11, 2016 20:34
NibLoadableView.swift
import UIKit
class NibLoadableView: UIView {
@IBOutlet var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
}
@HiddenJester
HiddenJester / Color+UIColor.swift
Last active April 23, 2022 10:34
A simple extension to SwiftUI Color that bridges over the "missing" colors from UIColor. Write `Text("Hello").foregroundColor(.label)` instead of `Text("Hello").foregroundColor(Color(UIColor.label))`
//
// Color+UIColor.swift
// Swatches
//
// Created by Timothy Sanders on 2019-11-15.
import Foundation
import SwiftUI
import UIKit
import SwiftUI
// Adapted from: https://stackoverflow.com/questions/62102647/swiftui-hstack-with-wrap-and-dynamic-height/62103264#62103264
struct WrappingHStack<Model, V>: View where Model: Hashable, V: View {
typealias ViewGenerator = (Model) -> V
var models: [Model]
var viewGenerator: ViewGenerator
var horizontalSpacing: CGFloat = 2
var verticalSpacing: CGFloat = 0
@EnesKaraosman
EnesKaraosman / RandomColor.swift
Last active July 14, 2023 09:35
Generatin random color in SwiftUI & UIKit
#if canImport(UIKit)
import UIKit
extension UIColor {
static var random: UIColor {
return UIColor(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
@iamjason
iamjason / us-states-array
Created December 29, 2014 01:31
Swift US States Array
let state = [ "AK - Alaska",
"AL - Alabama",
"AR - Arkansas",
"AS - American Samoa",
"AZ - Arizona",
"CA - California",
"CO - Colorado",
"CT - Connecticut",
"DC - District of Columbia",
"DE - Delaware",
@nbasham
nbasham / UIColor.swift
Last active December 18, 2023 21:04
Swift 4: Convert CSS color names and RGB hex values to UIColor. UIColor from hex 3, 4, 6, and 8 characters in length with or without # prefix. UIColor to hex. UIColor extension that creates immutable UIColor instances from hexadecimal and CSS color name strings (e.g. ff0, #f00, ff0000, ff0000ff, Pink, aZure, CLEAR, nil). Conversely, you can obta…
//
// UIColor.swift
// previously Color+HexAndCSSColorNames.swift
//
// Created by Norman Basham on 12/8/15.
// Copyright ©2018 Black Labs. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
@zappycode
zappycode / NSImageToJpeg.swift
Last active December 31, 2023 03:40
Coverting an NSImage into JPEG Data
func jpegDataFrom(image:NSImage) -> Data {
let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)!
let bitmapRep = NSBitmapImageRep(cgImage: cgImage)
let jpegData = bitmapRep.representation(using: NSBitmapImageRep.FileType.jpeg, properties: [:])!
return jpegData
}
@zacwest
zacwest / ios-font-sizes.swift
Last active March 27, 2024 07:16
iOS default font sizes - also available on https://www.iosfontsizes.com
let styles: [UIFont.TextStyle] = [
// iOS 17
.extraLargeTitle, .extraLargeTitle2,
// iOS 11
.largeTitle,
// iOS 9
.title1, .title2, .title3, .callout,
// iOS 7
.headline, .subheadline, .body, .footnote, .caption1, .caption2,
]