Skip to content

Instantly share code, notes, and snippets.

@unnamedd
Last active July 1, 2019 22:34
Show Gist options
  • Save unnamedd/19565c454930b04b331438c4ddb2e19f to your computer and use it in GitHub Desktop.
Save unnamedd/19565c454930b04b331438c4ddb2e19f to your computer and use it in GitHub Desktop.
[SwiftUI] Text style buttons
//
// ContentView.swift
// TestsWithSwiftUI
//
// Created by Thiago Holanda on 01.07.19.
// Copyright © 2019 unnamedd codes. All rights reserved.
//
import SwiftUI
struct ContentView : View {
@State var isBold: Bool = false
@State var isItalic: Bool = false
@State var isUnderline: Bool = false
@State var isStrikethrough: Bool = false
let size: TextStyleButton.Size = (width: 40, height: 30)
var body: some View {
HStack(spacing: 2.0) {
TextStyleButton(
state: $isBold,
textStyle: .bold,
size: size
)
TextStyleButton(
state: $isItalic,
textStyle: .italic,
size: size
)
TextStyleButton(
state: $isUnderline,
textStyle: .underline,
size: size,
offset: (x: 0, y: 2)
)
TextStyleButton(
state: $isStrikethrough,
textStyle: .strikethrough,
size: size
)
}.cornerRadius(5)
}
}
enum TextStyle: String, CaseIterable {
case bold
case italic
case strikethrough
case underline
}
struct TextStyleButton: View {
typealias Size = (width: Length, height: Length)
typealias Offset = (x: Length, y: Length)
@Binding var state: Bool
var textStyle: TextStyle
var size: Size
var offset: Offset = (x: 0, y: 0)
var body: some View {
Button(action: {
print("Toggle it - \(self.textStyle.rawValue)")
self.$state.value.toggle()
}) {
Image(systemName: textStyle.rawValue)
.imageScale(.medium)
.foregroundColor(self.state ? .white : .black)
.font(.headline)
.frame(width: size.width, height: size.height)
.offset(x: self.offset.x, y: self.offset.y)
}
.background(self.state ? Color.gray : Color.yellow)
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
.previewLayout(.fixed(width: 200, height: 50))
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment