• 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