Skip to content

Instantly share code, notes, and snippets.

View xiaoxidong's full-sized avatar
🎯
Focusing

XiaoDong xiaoxidong

🎯
Focusing
View GitHub Profile
@wassupdoc
wassupdoc / popoversample
Last active May 7, 2021 04:45
SwiftUI Popover
class PopupViewController:UIViewController{
let frame:CGRect
let content:AnyView
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
@SatoTakeshiX
SatoTakeshiX / scaleAndOrient.swift
Created May 17, 2020 07:13
Scale and Orient Image
func scaleAndOrient(image: UIImage) -> UIImage {
// Set a default value for limiting image size.
let maxResolution: CGFloat = 640
guard let cgImage = image.cgImage else {
print("UIImage has no CGImage backing it!")
return image
}
@mecid
mecid / Calendar.swift
Last active May 15, 2025 03:19
SwiftUI Calendar view using LazyVGrid
import SwiftUI
extension Calendar {
func generateDates(
inside interval: DateInterval,
matching components: DateComponents
) -> [Date] {
var dates: [Date] = []
dates.append(interval.start)
@unnamedd
unnamedd / PreviewDevice+ListOfPads.swift
Created April 25, 2020 18:19
A small extension for PreviewDevice to avoid enter the values in the SwiftUI previews manually
/**
* PreviewDevice+ListOfPads.swift
* Copyright (c) Thiago Holanda 2020
* https://twitter.com/tholanda
*
* MIT license
*/
import SwiftUI
import SwiftUI
struct ContentView: View {
@State var gradient = [
Color.red, .orange, .yellow, .green, .blue, .purple, Color.red, .orange, .yellow, .green, .blue, .purple,
]
@State var startPoint = UnitPoint(x: 0, y: -1)
@State var endPoint = UnitPoint(x: 0, y: 1)
@jmcd
jmcd / Dnd.swift
Created February 14, 2020 18:21
Checking out drag n' drop in Swift UI
import SwiftUI
struct AnimalView: View {
var model: Animal
var body: some View {
VStack {
Image(systemName: model.systemImageName)
.resizable()
.scaledToFit()
@jmcd
jmcd / MacOSDrawPNG.swift
Created January 24, 2020 08:45
Drawing to a PNG
func makePNG(_ size: NSSize, by drawing: (CGContext) -> ()) -> Data? {
guard let bitmapImageRep = NSBitmapImageRep(bitmapDataPlanes: nil,
pixelsWide: Int(size.width),
pixelsHigh: Int(size.height),
bitsPerSample: 8,
samplesPerPixel: 4,
hasAlpha: true,
isPlanar: false,
colorSpaceName: NSColorSpaceName.deviceRGB,
bytesPerRow: 0, bitsPerPixel: 0) else {
@SatoTakeshiX
SatoTakeshiX / convert.swift
Last active June 6, 2024 12:25
Take capture a view by SwiftUI
//
// ContentView.swift
// TryGeometryReader
//
// Created by satoutakeshi on 2019/12/07.
// Copyright © 2019 satoutakeshi. Licensed under MIT.
//
import SwiftUI
@unnamedd
unnamedd / MacEditorTextView.swift
Last active May 27, 2025 14:38
[SwiftUI] MacEditorTextView - A simple and small NSTextView wrapped by SwiftUI.
/**
* MacEditorTextView
* Copyright (c) Thiago Holanda 2020-2021
* https://bsky.app/profile/tholanda.com
*
* (the twitter account is now deleted, please, do not try to reach me there)
* https://twitter.com/tholanda
*
* MIT license
*/
@timothycosta
timothycosta / UIViewController+SwiftUI.swift
Last active November 9, 2024 23:09
Using UIViewController via the SwiftUI Environment
struct ViewControllerHolder {
weak var value: UIViewController?
init(_ value: UIViewController?) {
self.value = value
}
}
struct ViewControllerKey: EnvironmentKey {
static var defaultValue: ViewControllerHolder { return ViewControllerHolder(UIApplication.shared.windows.first?.rootViewController ) }