安装
LabelSwitch 通过 CocoaPods 提供使用。要安装它,只需将以下行添加到 Podfile 中
pod 'LabelSwitch'
用法
您可以通过代码或 InterfaceBuilder 来创建视图
使用 Interface Builder
为开关设置位置约束,并将宽度和高度约束设置为零以上。
使用代码
let ls = LabelSwitchConfig(text: "Text1",
textColor: .white,
font: UIFont.boldSystemFont(ofSize: 15),
backgroundColor: .red)
let rs = LabelSwitchConfig(text: "Text2",
textColor: .white,
font: UIFont.boldSystemFont(ofSize: 20),
backgroundColor: .green)
// Set the default state of the switch,
let labelSwitch = LabelSwitch(center: .zero, leftConfig: ls, rightConfig: rs)
// Set the appearance of the circle button
labelSwitch.circleShadow = false
labelSwitch.circleColor = .red
// Make switch be triggered by tapping on any position in the switch
labelSwitch.fullSizeTapEnabled = true
// Set the delegate to inform when the switch was triggered
labelSwitch.delegate = self
extension ViewController: LabelSwitchDelegate {
func switchChangToState(sender: LabelSwitch) {
switch sender.curState {
case .L: print("left state")
case .R: print("right state")
}
}
}
您也可以使开关背景为图像或渐变色
// gradient color
init(text: String, textColor: UIColor, font: UIFont, gradientColors: [CGColor], startPoint: CGPoint, endPoint: CGPoint)
// image
init(text: String, textColor: UIColor, font: UIFont, image: UIImage?)
作者:
许可证
LabelSwitch可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。
由@Tobaloidee设计标志