All Projects → eneko → ConsoleUI

eneko / ConsoleUI

Licence: other
Rasterize SwiftUI views to images from macOS terminal

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to ConsoleUI

Swiftuix
Extensions and additions to the standard SwiftUI library.
Stars: ✭ 4,087 (+7759.62%)
Mutual labels:  swift-package-manager, swiftui
Dikit
Dependency Injection Framework for Swift, inspired by KOIN.
Stars: ✭ 77 (+48.08%)
Mutual labels:  swift-package-manager, swiftpm
Rock
With Rock you can easily install CLIs built with Swift Package Manager. Prefer vknabel/Archery and yonaskolb/Mint instead
Stars: ✭ 13 (-75%)
Mutual labels:  swift-package-manager, swiftpm
Laden
SwiftUI loading indicator view
Stars: ✭ 23 (-55.77%)
Mutual labels:  swift-package-manager, swiftui
SwiftDown
📦 A themable markdown editor component for your SwiftUI apps.
Stars: ✭ 203 (+290.38%)
Mutual labels:  swift-package-manager, swiftui
CurrencyText
Currency text field formatter available for UIKit and SwiftUI 💶✏️
Stars: ✭ 124 (+138.46%)
Mutual labels:  swift-package-manager, swiftui
Scenekit Scnline
Draw a tube or thick line in SceneKit
Stars: ✭ 49 (-5.77%)
Mutual labels:  swift-package-manager, swiftpm
Shift
Light-weight EventKit wrapper.
Stars: ✭ 31 (-40.38%)
Mutual labels:  swift-package-manager, swiftui
EggSeed
Command Line Tool for Starting Your Swift Packages with Continuous Integration
Stars: ✭ 21 (-59.62%)
Mutual labels:  swift-package-manager, swiftpm
Swipycell
Easy to use UITableViewCell implementing swiping to trigger actions.
Stars: ✭ 230 (+342.31%)
Mutual labels:  swift-package-manager, swiftpm
vintage
[UNMAINTED] command-line tool to check for outdated Swift Package Manager dependencies
Stars: ✭ 33 (-36.54%)
Mutual labels:  swift-package-manager, swiftpm
ResponderChain
Cross-platform first responder handling without subclassing views or making custom ViewRepresentables in SwiftUI. Similar to FocusState but for iOS 13+
Stars: ✭ 69 (+32.69%)
Mutual labels:  swift-package-manager, swiftui
tapit-app
App which lets two people share their social media details by simply putting one phone on top of the other ("tapping"). Currently in development by Nikita Mounier.
Stars: ✭ 18 (-65.38%)
Mutual labels:  swift-package-manager, swiftui
Realityui
A Swift Package for creating familiar UI Elements and animations in a RealityKit rendered Augmented Reality or Virtual Reality scene.
Stars: ✭ 275 (+428.85%)
Mutual labels:  swift-package-manager, swiftpm
column-text-view-ui
📄 Column Text View is an adaptive UI component that renders text in columns, horizontally [iOS 12, UIKit, TextKit, SwiftUI].
Stars: ✭ 11 (-78.85%)
Mutual labels:  swift-package-manager, swiftui
Scenekit Bezier Animations
Create animations over Bezier curves of any number of points
Stars: ✭ 35 (-32.69%)
Mutual labels:  swift-package-manager, swiftpm
SwiftUI-Shapes
Commonly Used Shapes and Utilities In SwiftUI
Stars: ✭ 52 (+0%)
Mutual labels:  swift-package-manager, swiftui
PackageBuilder
PackageBuilder builds a simple command-line structure by SwiftPM.
Stars: ✭ 27 (-48.08%)
Mutual labels:  swift-package-manager, swiftpm
Swift Docker
Build & test your swift packages using docker - `swift docker test`
Stars: ✭ 179 (+244.23%)
Mutual labels:  swift-package-manager, swiftpm
SwiftCurrent
A library for managing complex workflows in Swift
Stars: ✭ 286 (+450%)
Mutual labels:  swift-package-manager, swiftui

ConsoleUI

Demo repository to showcase how to programmatically generate images from a command-line tool with SwiftUI.

The Basics: Hello, world!

Create a new command-line executable

$ mkdir mytool && cd mytool
$ swift package init --type executable
$ swift run
Hello, world!

Generate Xcode project (optional)

Xcode does not support live previews on Swift packages yet (FB8979344). To allow for realtime previews follow these steps:

  1. Generate an Xcode project
  2. Add a macOS target to the project
  3. Add your view (see next section) to the macOS target
$ swift package generate-xcodeproj
$ open mytool.xcodeproj

Add SwiftUI view

Create a view with a red label over white background

import SwiftUI

struct MySwiftUIView : View {
    var body: some View {
        ZStack {
            Color.blue.edgesIgnoringSafeArea(.all)
            Text("Hello, world!")
                .foregroundColor(.white)
                .font(.largeTitle)
        }
    }
}

#if DEBUG
struct MySwiftUIView_Previews : PreviewProvider {
    static var previews: some View {
        MySwiftUIView()
    }
}
#endif

SwiftUI

Rasterize view and save to file

We will use this method to rasterize views to images:

func rasterize(view: NSView, format: NSBitmapImageRep.FileType) -> Data? {
    guard let bitmapRepresentation = view.bitmapImageRepForCachingDisplay(in: view.bounds) else {
        return nil
    }
    bitmapRepresentation.size = view.bounds.size
    view.cacheDisplay(in: view.bounds, to: bitmapRepresentation)
    return bitmapRepresentation.representation(using: format, properties: [:])
}

The following code instantiates the SwiftUI view, wrapped inside a NSHostingView which is then rasterized and saved to disk

let wrapper = NSHostingView(rootView: MySwiftUIView())
wrapper.frame = CGRect(x: 0, y: 0, width: 800, height: 450)

let png = rasterize(view: wrapper, format: .png)
try png?.write(to: URL(fileURLWithPath: "test.png"))

main.swift

Run the command and open in Preview

$ swift run && open test.png

Preview

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].