Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jordibruin/7d0c24d0268bd7aadc5a0203f33583ae to your computer and use it in GitHub Desktop.
Save jordibruin/7d0c24d0268bd7aadc5a0203f33583ae to your computer and use it in GitHub Desktop.
BookCover generator for Paper app
//
// ContentView.swift
// BookCover
//
// Created by Jordi Bruin on 01/07/2022.
//
import SwiftUI
struct ContentView: View {
@State var title: String = "Title"
@State var color: Color = .blue
@State var textColor: Color = .white
@State var textSize: Double = 80
@State var showAlert = false
var body: some View {
NavigationView {
left
right
}
.edgesIgnoringSafeArea(.all)
.alert(isPresented: $showAlert) {
Alert(title: Text("Saved"))
}
}
var left: some View {
Form {
Section {
TextField("Title", text: $title)
}
Section {
ColorPicker("Background Color", selection: $color)
Button {
color = Color.random
} label: {
Text("Random")
}
} header: {
Text("Background Color")
}
Section {
ColorPicker("Text Color", selection: $textColor)
Slider(value: $textSize, in: 40...150) { edited in
print("edited")
}
Button {
textColor = .white
} label: {
Text("White")
}
Button {
textColor = .black
} label: {
Text("Black")
}
Button {
textColor = Color.random
} label: {
Text("Random")
}
} header: {
Text("Text Color")
}
Section {
Button {
textColor = Color.random
color = Color.random
} label: {
Label("Generate Random", systemImage: "paintbrush.fill")
}
}
Button {
makeImage()
} label: {
Label("Generate Image", systemImage: "paintpalette.fill")
.foregroundColor(textColor)
}
.listRowBackground(color)
}
.navigationViewStyle(.columns)
.navigationTitle("Book Cover")
.frame(width: 350)
}
var right: some View {
ZStack {
Color(.systemBackground)
cover()
.overlay(
HStack {
Rectangle()
.foregroundColor(.black.opacity(0.1))
.frame(width: 80)
Spacer()
}
)
.scaleEffect(0.9)
}
}
func cover(rounded: Bool = true) -> some View {
RoundedRectangle(cornerRadius: rounded ? 30 : 0)
.frame(
width: 500,
height: 800
)
.foregroundColor(color)
.overlay(
VStack {
Spacer()
Text(title.uppercased())
.font(Font.custom("Roc Grotesk", size: textSize))
.foregroundColor(textColor)
.padding()
.multilineTextAlignment(.center)
// .background(Color.red)
.offset(x: 40)
Spacer()
}
.padding(.vertical, 80)
)
}
@MainActor func makeImage() {
guard let image = ImageRenderer(content: cover(rounded: false)).uiImage else { return }
let renderer = ImageRenderer(content: cover(rounded: false))
renderer.scale = 3
guard let image = renderer.uiImage else { return }
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
print("Saved")
showAlert = true
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
extension Color {
static var random: Color {
return Color(
red: .random(in: 0...1),
green: .random(in: 0...1),
blue: .random(in: 0...1)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment