RadioGroup
缺失的iOS单选按钮组。
用法
let radioGroup = RadioGroup(titles: ["First Option Title", "Another Option Title", "Last"])
radioGroup.selectedIndex = 0
radioGroup.addTarget(self, action: #selector(optionSelected), forControlEvents: .valueChanged)
SwiftUI用法
RadioGroupPicker(selectedIndex: $selection, titles: ["One", "Two", "Three"])
.fixedSize()
更改外观
以下属性可以在Interface Builder中设置,或在代码中设置,或通过UIAppearance
代理(例如,RadioGroup.appearance().titleColor = .red
)。当使用SwiftUI时,您可以在RadioGroupPicker
初始化器中设置它们,或用作视图修饰符(例如,.buttonSize(32)
)。
布局
radioGroup.isVertical = false // default is true => buttons are stacked vertically
radioGroup.titleAlignment = .right
radioGroup.isButtonAfterTitle = true // default is false => button left (leading) relative to title
颜色和字体
radioGroup.tintColor = .green // surrounding ring
radioGroup.selectedColor = .red // inner circle (default is same color as ring)
radioGroup.selectedTintColor = .blue // selected radio button's surrounding ring (default is tintColor)
radioGroup.titleColor = .blue
radioGroup.titleFont = myFont
或使用属性文本
radioGroup.attributedTitles = [
NSAttributedString(string: "Italic", attributes: [.obliqueness: 0.3]),
NSAttributedString(string: "Underline", attributes: [.underlineStyle: 1]),
]
大小
radioGroup.buttonSize = 42.0
radioGroup.spacing = 12 // spacing between buttons
radioGroup.itemSpacing = 12 // horizontal spacing between button and title
按钮外观
RadioButton.appearance().size = 32 // height=width of button
RadioButton.appearance().ringWidth = 3
RadioButton.appearance().ringSpacing = 7 // space between outer ring and inner circle
RadioButton.appearance().selectedColor = .blue // color of inner circle
安装
CocoaPods
pod 'RadioGroup'
Swift Package Manager
dependencies: [
.package(url: "https://github.com/yonat/RadioGroup", from: "1.4.3")
]