Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
🗳 CMRadioButtonGroup 🗳
Radio Button Group Support Generic Type of Value for SwiftUI
🏁 Getting Started
Requirements
- Xcode 11+
- SwiftUI
- iOS 14+
- macOS 10.15+
Installaion
Swift Package Manager(SPM)
File ➜ Swift Packages ➜ Add Package Dependancy..
.package(url: "https://github.com/Changemin/CMRadioButtonGroup", from: "1.0.1")
🎈Usage
@State var value: T = 0
var options: [CMRadioOption<T>] = [
CMRadioOption(label: "Option 1", value: 1),
CMRadioOption(label: "Option 2", value: 2),
CMRadioOption(label: "Option 3", value: 3)
]
CMRadioButtonGroup(options: options, value: $value)
options
: Array of the available optionslabel
: Label displays beyond the buttonvalue
: Actually value of the option
You can change variable type
T
into any type you want. (even custom enum)
🛠 Custom Modifiers
CMRadioButtonGroup(options: options, value: $value)
.accentColor(color: Color)
color
: accentColor of the button
Example
👶 Simple
import CMRadioButtonGroup
struct ContentView: View {
@State var value: Int = 0
var options: [CMRadioOption<Int>] = [
CMRadioOption(label: "Option 1", value: 1),
CMRadioOption(label: "Option 2", value: 2),
CMRadioOption(label: "Option 3", value: 3)
]
var body: some View {
VStack {
CMRadioButtonGroup(options: options, value: $value)
}
}
}
Result
🎨 Accent Color, Other value type
import CMRadioButtonGroup
struct ContentView: View {
@State var value: Float = 0
var options: [CMRadioOption<Float>] = [
CMRadioOption(label: "Option 1(1.1)", value: 1.1),
CMRadioOption(label: "Option 2(1.2)", value: 1.2),
CMRadioOption(label: "Option 3(1.3)", value: 1.3)
]
var body: some View {
VStack {
CMRadioButtonGroup(options: options, value: $value)
.accentColor(.orange)
}
}
}
Result
📚 Advanced, Custom type(enum)
import CMRadioButtonGroup
enum MyEnum {
case case1
case case2
case case3
}
struct ContentView: View {
@State var value: MyEnum = .case1
var options: [CMRadioOption<MyEnum>] = [
CMRadioOption(label: "MyEnum : case1", value: .case1),
CMRadioOption(label: "MyEnum : case1", value: .case2),
CMRadioOption(label: "MyEnum : case1", value: .case3)
]
var body: some View {
CMRadioButtonGroup(options: options, value: $value)
}
}
Result
📜 License
CMRadioButtonGroup is available under the MIT license. See the LICENSE
file for more info.
✅ Todos
- ☐ design variation
- ☐ more modifiers
- ☐ custom font
✍️ Author
- Byun Kyung Min ➜ 🇰🇷@Changemin
Github
link |
Stars: 0 |
You may find interesting
Releases
Version 1.0.1 - 2021-01-15T11:52:01
Remove 'idx' in CMRadioOption