Coder Social home page Coder Social logo

labelswitch's Introduction

LabeSwitch

Build Status Version License Platform

Installation

LabelSwitch is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'LabelSwitch'

Usage

You can create the view through code or InterfaceBuilder

Use with Interface Builder

Set the position constarint for the switch, and set the width and height constraint to larger than zero.

Use with code

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")
        }
    }
}

you can also make the switch background to be image or gradient color

// 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?)

Author

[email protected]

License

LabelSwitch is available under the MIT license. See the LICENSE file for more info.

logo by @Tobaloidee

labelswitch's People

Contributors

cookiezby avatar dingtianran avatar tobaloidee avatar zbydonuts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

labelswitch's Issues

IB Crash

The Interface Builder crashes with the following error:
Pods/LabelSwitch/LabelSwitch/Classes/LabelSwitch.swift: 63: 28: Fatal error: Use of unimplemented initializer 'init(frame:)' for class 'LabelSwitch.LabelSwitch'

I think the only thing required is to implement init(frame:). It could be as simple as this:

public override init(frame: CGRect) {
    self.init(center: .zero, leftConfig: .defaultLeft, rightConfig: .defaultRight)
}

Set State on screen load

I want to set the current state of the control as Right side (Text = Left). On click of the control it works. But I want when screen load.

I have tried this with the property "curState = .L/.R" but it didn't work for me.

Allow option to disable switch

Native iOS interactive elements, like the UISwitch, inherit from UIControl, which allows them to be disabled. While your LabelSwitch likely can't inherit from UIControl, it would be nice if there was an option to disable the switch to prevent interaction. A couple of use cases:

  • A switch triggers a network request. You could disable the switch while the request is active to prevent switch updates by the user.
  • A switch is used to determine the user's choice of a feature or service that might not be eligible to them. Thus, you would want to disable the switch so they can't use it.

wrong LabelSwitchDelegate output

in your file LabelSwitch.swift yo have wrong order in
func switchTaped(sender: Any)

your one:

 @objc
    func switchTaped(sender: Any) {
        UIView.animate(withDuration: 0.3) {
            switch self.curState {
            case .L:
                self.delegate?.switchChangToState(sender: self)
                self.curState = .R
            case .R:
                self.delegate?.switchChangToState(sender: self)
                self.curState = .L
            }
        }
    }

you need to call delegate after changing curState. Otherwise the
func switchChangToState(sender: LabelSwitch) gives you wrong output

ideally, I'd suggest:

    func switchTaped(sender: Any) {
        UIView.animate(withDuration: 0.3, animations: {
            switch self.curState {
            case .L:
                self.curState = .R
            case .R:
                self.curState = .L
            }
        }) { (completed) in
            if completed {
                self.delegate?.switchChangToState(sender: self)
            }
        }
    }

Transparent UIImage

Hi, is it possible to programmatically set a png image with 0.5 alpha to LabelSwitchConfig?
Thanks for help.

Cannot assign more than one switch to a delegate

Correct me if I'm wrong, but in the current implementation it is impossible to distinguish which LabelSwitch has changed state. For example if I have 3 switches in the view and assign the same delegate for each, I will not be able to determine which switch triggered the event. Can you return the LabelSwitch that triggered the event via the delegate?

Designables: Error

I'm facing some issues.

Designables: Error

How to fix this issue, please help me out.

Thanks

Screen Shot 2021-09-23 at 4 23 42 PM

Logo Proposal

Good day @Cookiezby , I am Tobaloidee and i am a graphics designer. I want to contribute here in your project by designing a logo for it which i will be doing for free. With your permission i will start my design and show you here asap. Thanks! - Tobaloidee

Should be able to center switch in the View when adding in code

When adding programmatically the code starts at the center of the view, but if I want the switch to be centered it has to be manually adjusted by
LabelSwitch(center: CGPoint(x: self.switchStackView.center.x - X, y: self.switchStackView.center.y), leftConfig: ls, rightConfig: rs)

attempting to adjust center when calculating center
self.center.x = self.center.x - diameter - 2 * circlePadding, this is still missing about 8-10 points that should be offset but not sure why

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.