Swiftpack.co - kieranb662/SegmentedPicker as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by kieranb662.
kieranb662/SegmentedPicker 0.0.2
SwiftUI Segmented Picker. Create vertical and horizontal pickers and style them to your liking
⭐️ 14
🕓 1 year ago
iOS
.package(url: "https://github.com/kieranb662/SegmentedPicker.git", from: "0.0.2")

Segmented Picker Logo

SwiftUI Swift 5.1 Swift 5.1 kieranb662 followers

Segmented Picker Example Gif

Use the SegmentedPicker library to create custom vertical or horizontal SegmentedPickers. Follow along with the examples below to get started quickly.

import SwiftUI
import SegmentedPicker
import Shapes


struct LineCapPicker: View {
    @State var lineCap: LineCap = .butt
    
    
    enum LineCap: String, CaseIterable, Identifiable {
        case butt = "butt"
        case round = "round"
        case square = "square"
        
        var id: String { self.rawValue }
        
        var cgLineCap: CGLineCap {
            switch self {
            case .butt:
                return .butt
            case .round:
                return .round
            case .square:
                return .square
            }
        }
    }
    
    var body: some View {
        SegmentedPicker($lineCap, LineCap.allCases, isVertical: true) { (cap: LineCap)   in
            HorizontalLine()
                .stroke(Color(white: 0.85), style: StrokeStyle(lineWidth: 20, lineCap: cap.cgLineCap))
                .frame(width: 40)
            
        }.overlay(RoundedRectangle(cornerRadius: 5).stroke(Color.green))
            .frame(height: 120)
    }
}


struct RGBColorSpacePicker: View {
    let colorSpaces: [String] = ["sRGB", "sRGB Linear", "Display P3"]
    @State var selected: String = "sRGB"
    
    var body: some View {
        SegmentedPicker($selected, colorSpaces) { (text: String)   in
            Text(text)
        }.overlay(RoundedRectangle(cornerRadius: 5).stroke(Color.green))
    }
}

struct TextAlignmentPicker: View {
    @State var alignment: TextAlignmentEnum = .left
    
    enum TextAlignmentEnum: String, CaseIterable, Identifiable {
        case left = "text.alignleft"
        case center = "text.aligncenter"
        case right = "text.alignright"
        var id: String {rawValue}
        
        var alignment: TextAlignment {
            switch self {
            case .left:
                return .leading
            case .center:
                return .center
            case .right:
                return .trailing
            
            }
        }
    }
    
    
    var body: some View {
        SegmentedPicker($alignment, TextAlignmentEnum.allCases) { (alignment)   in
            Image(systemName: alignment.rawValue)
        }.overlay(RoundedRectangle(cornerRadius: 5).stroke(Color.green))
    }
}


struct SegmentedPickerExample: View {
    
    var body: some View {
        ZStack {
            Color(white: 0.1)
                .edgesIgnoringSafeArea(.all)
            VStack {
                Text("RGB Color Space").padding()
                RGBColorSpacePicker()
                Divider()
                Text("Text Alignment").padding()
                TextAlignmentPicker()
                Divider()
                Text("Line Cap").padding()
                LineCapPicker()
            }
        }.navigationBarTitle("Segmented Picker")
        
    }
}

struct ContentView: View {
    var body: some View {
        SegmentedPickerExample()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().colorScheme(.dark)
    }
}

GitHub

link
Stars: 14
Last commit: 1 year ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Related Packages

Release Notes

Added platforms for archive builds
1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics