Skip to content

Instantly share code, notes, and snippets.

View WrappingHStack.swift
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 Jul 11, 2021
Generatin random color in SwiftUI & UIKit
View RandomColor.swift
#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)
)
@HiddenJester
HiddenJester / Color+UIColor.swift
Last active May 1, 2021
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))`
View Color+UIColor.swift
//
// Color+UIColor.swift
// Swatches
//
// Created by Timothy Sanders on 2019-11-15.
import Foundation
import SwiftUI
import UIKit
@benhurott
benhurott / notificationService.swift
Created Mar 31, 2017
Swift :: Service for Notifications
View notificationService.swift
import Foundation
class NotificationService {
static private var _instance: NotificationService! = nil
static var sharedInstance: NotificationService {
get {
if NotificationService._instance == nil {
NotificationService._instance = NotificationService()
@zappycode
zappycode / NSImageToJpeg.swift
Last active Jul 20, 2020
Coverting an NSImage into JPEG Data
View NSImageToJpeg.swift
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
}
View NibLoadableView.swift
import UIKit
class NibLoadableView: UIView {
@IBOutlet var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
}
@zacwest
zacwest / ios-font-sizes.swift
Last active Jul 22, 2021
iOS default font sizes - also available on https://www.iosfontsizes.com
View ios-font-sizes.swift
let styles: [UIFont.TextStyle] = [
// iOS 11
.largeTitle,
// iOS 9
.title1, .title2, .title3, .callout,
// iOS 7
.headline, .subheadline, .body, .footnote, .caption1, .caption2,
]
for style in styles {
@nbasham
nbasham / UIColor.swift
Last active Jul 8, 2021
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…
View UIColor.swift
//
// 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
@Pretz
Pretz / uiviewcontroller-playground.swift
Created Dec 8, 2015
UIViewController Playground: because getting a UIViewController/UINavigationController to display properly in a playground is a little non-obvious
View uiviewcontroller-playground.swift
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)
@iamjason
iamjason / us-states-array
Created Dec 29, 2014
Swift US States Array
View 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",