iOS 平台缺失的单选按钮组组件。
let radioGroup = RadioGroup(titles: ["First Option Title", "Another Option Title", "Last"])
radioGroup.selectedIndex = 0
radioGroup.addTarget(self, action: #selector(optionSelected), forControlEvents: .valueChanged)
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
pod 'RadioGroup'
dependencies: [
.package(url: "https://github.com/yonat/RadioGroup", from: "1.4.9")
]