Giter VIP home page Giter VIP logo

placeholders's Introduction

Placeholders

Swift Platform

Placeholders gives you the ability to define multiple placeholders for UITextField, and also animate their change in the way you like. The result looks like that:

Demo

You can read more about the library on Medium.

Usage

1. Define a Placeholder object in your view controller:

let placeholders = Placeholders(placeholders: ["First", "Second", "Third"])

If you want to loop placeholders (make the set infinite):

let placeholders = Placeholders(placeholders: ["First", "Second", "Third"], options: .infinite)

If you also want to show them in a random order:

let placeholders = Placeholders(placeholders: ["First", "Second", "Third"], options: [.infinite, .shuffle])

2. In your viewWillAppear method, call .start:

placeholders.start(interval: 3.0,
                   fireInitial: true,
                   textField: textField,
                   animation: .pushTransition(.fromBottom))

That's it!

Advanced

While being easy-to-use, Placeholders can be highly customized. At it's core, Placeholders object doesn't know anything about UITextField. You can easily use it for other purposes if you wish:

let placeholders = Placeholders(placeholders: ["A", "B", "C"], options: .infinite)
placeholders.start(interval: 2.0, fireInitial: true, action: { next in
    print(next)
})

Actually, the UITextField convenience is just a wrapper around this method.

But if you just want to make a custom animation as, for example, .pushTransition, you can extend UITextField.PlaceholderChange. Here is how you can implement your own custom fade animation:

extension UITextField.PlaceholderChange {
    
    static var fade: UITextField.PlaceholderChange<Placeholder> {
        return UITextField.PlaceholderChange { (placeholder, textField) in
            let transition = CATransition()
            transition.duration = 0.35
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = kCATransitionFade
            textField.subviews.first(where: { NSStringFromClass(type(of: $0)) == "UITextFieldLabel" })?.layer.add(transition, forKey: nil)
            placeholder.set(on: textField)
        }
    }
    
}

This generic Placeholder type and placeholder.set(on: textField) syntax exists in order to support NSAttributedString as a placeholder.

You can also use convenience .caTransition static function to make your life a bit easier:

extension UITextField.PlaceholderChange {
    
    static var fade: UITextField.PlaceholderChange<Placeholder> {
        return .caTransition {
            let transition = CATransition()
            transition.duration = 0.35
            transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
            transition.type = kCATransitionFade
            return transition
        }
    }
    
}

And now you can simply write:

placeholders.start(interval: 3.0,
                   fireInitial: true,
                   textField: textField,
                   animation: .fade)

Neat!

Installation

Placeholders is available through Carthage. To install, just write into your Cartfile:

github "dreymonde/Placeholders" ~> 0.1.0

We also encourage you to write your very own implementation that fits your needs best. Our source code is there to help.

placeholders's People

Contributors

dreymonde avatar

Watchers

James Cloos avatar

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.