• Stars
    star
    199
  • Rank 196,105 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ…ΏοΈ Define multiple placeholders for UITextField and animate their change

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.

More Repositories

1

Time

πŸ•° Type-safe time calculations in Swift
Swift
1,073
star
2

AppFolder

πŸ—‚ Never use NSSearchPathForDirectoriesInDomains again
Swift
938
star
3

Delegated

πŸ‘·β€β™€οΈ Closure-based delegation without memory leaks
Swift
703
star
4

Shallows

πŸ›Ά Your lightweight persistence toolbox
Swift
622
star
5

NiceNotifications

πŸ”” Create rich local notifications experiences on iOS with incredible ease
Swift
269
star
6

ScheduledNotificationsViewController

See all your scheduled local notifications in one place
Swift
170
star
7

DateBuilder

πŸ“† Create dates and date components easily (e.g. "first Thursday of the next month")
Swift
136
star
8

TheGreatGame

πŸ† Open-source first-class iOS & watchOS app dedicated to Women’s Euro 2017
Swift
135
star
9

Paperville

πŸ™ Design a city in Swift code (ο£ΏWWDC 2018 submission, ACCEPTED)
Swift
51
star
10

TelegraphKit

πŸ“œ The ultimate solution for showing ad hoc, server-editable web content (FAQs, Tutorials, Privacy Policy, etc.) in your iOS apps
Swift
51
star
11

DonateToUkraine

πŸ‡ΊπŸ‡¦ Implement "donate to Ukraine" inside your app, with Apple Pay
Swift
25
star
12

Alba

πŸŽ™ Stateful event observing engine [DEPRECATED]
Swift
19
star
13

Avenues

πŸŒ… [WIP] Idiomatic image fetching and caching in Swift.
Swift
13
star
14

Subviews

🧩 @​Subview and other ways of making UIKit more fun to use
Swift
9
star
15

Timers

⏲️ Intuitive Swift timers with automatic memory management
Swift
8
star
16

SwiftyNURE

Swift framework for NURE API (CIST)
Swift
3
star
17

Operacjas

πŸ›  [DEPRECATED] Unlocking the full glory of NSOperations
Swift
2
star
18

Swift-hints

1
star
19

DynamicInstance

Swift
1
star
20

Operations

[WIP] NSOperations for 2018
Swift
1
star
21

uahelp-js-scripts

JavaScript
1
star
22

Avenues-Shallows

Making caching even better
Swift
1
star
23

SofarKit

Access Sofar admin data with Swift [WIP]
Swift
1
star
24

Light

πŸ•Š Super thin networking layer built on top of Shallows
Swift
1
star