Animation label text changed from bottom to top in swift năm 2024

CountableLabel is a UILabel that was heavily inspired by GCountableUILabel. This subclass will provided a variety of nice text setting animations especially when the label is used for dynamic number changes. The reason we built our own version of GCountableUILabel is because the original repo was written in Objective-C, on top of that decrementing/setting of different size text cause a lot of breaking in the pod. With the new version you are free to change the text to what ever you choose! Happy Counting!

Show

Contents

Features

  • Works with all devices running iOS 9+.
  • Override text internally to keep the usage extremely simple.
  • 3 types of animations (PushUp, PushDown, Fade) and a feature have no animation as well.
  • Works with autolayout, honors intrinsic sizing, and self sizes on the fly.
  • Works with labels created in code and in Interface Builder.
  • Example app with lots of real-time configurations that lets you dynamically increment/decrement a value of your choosing, specify which animation you want to use, and specify text alignment of the label!

Usage

Set up

Swap any UILabel with CountableLabel:

In code


Before:

class ViewController { let label = UILabel() }

After:

import CountableLabel class ViewController { let label = CountableLabel() }

In Interface Builder


Before:

Animation label text changed from bottom to top in swift năm 2024

After:

Animation label text changed from bottom to top in swift năm 2024

Animation Type

Set

import CountableLabel class ViewController { let label = CountableLabel() }

0 to see text go bottom up across the label.

Animation label text changed from bottom to top in swift năm 2024

Set

import CountableLabel class ViewController { let label = CountableLabel() }

1 to see text go top down across the label.

Animation label text changed from bottom to top in swift năm 2024

Set

import CountableLabel class ViewController { let label = CountableLabel() }

2 to see text fade in and out on the label.

Animation label text changed from bottom to top in swift năm 2024

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects.

CocoaPods 0.36 adds supports for Swift and embedded frameworks. You can install it with the following command:

To integrate CountableLabel into your Xcode project using CocoaPods, specify it in your

import CountableLabel class ViewController { let label = CountableLabel() }

4:

Then, run the following command:

In case Xcode complains ("Cannot load underlying module for CountableLabel") go to Product and choose Clean (or simply press ⇧⌘K).

Manually

If you prefer not to use CocoaPods, you can integrate CountableLabel into your project manually.

License

CountableLabel is developed by Andrew Foghel at StockX and is released under the MIT license. See the

import CountableLabel class ViewController { let label = CountableLabel() }

7 file for details.

Contact

Feel free to email me (at ). If you find any problems with the project or have ideas to enhance it, feel free to open a GitHub issue and/or create a pull request.

So recently, I had to implement a Gmail style floating label text field in an app. The floating label input style has been around for quite a while since it was created by designer Matt D Smith. Let's take a look at the form below

The float label form interaction by Matt D Smith

The interaction is quite simple, float the label above the input field when the field is selected and remove the placeholder. When the field loses focus, bring back the placeholder and remove the label if the field is empty. Well, iOS has a default text field; UITextField which we can customise to achieve this effect. There are two ways to go about this, with storyboards and without. Let's get to work!!!

Step 1: Create a swift file to hold our customization code. For the sake of this tutorial, we’ll call it “FloatinLabelInput” (FloatingLabelInput.swift). This file will hold the code responsible for creating and animating the floating label. Create a class using the same name extending UITextField like in the code below:

FloatingLabelInput.swift

Next, we need to create our custom values and initializers to allow us to override the default UITextField implementation. We will be marking the values with the “@IBInspectable” keyword so we can set them in our interface builder; for those who love to use storyboards.

First, our values, copy and paste the code below into your FloatingLabelInput class

var floatingLabel: UILabel = UILabel(frame: CGRect.zero) // Label var floatingLabelHeight: CGFloat = 14 // Default height*@IBInspectable* var _placeholder: String? // we cannot override 'placeholder'@IBInspectable var floatingLabelColor: UIColor = UIColor.black {

**didSet** {  
    **self**.floatingLabel.textColor = floatingLabelColor  
    **self**.setNeedsDisplay()  
}  
}@IBInspectable var activeBorderColor: UIColor = UIColor.blue*@IBInspectable* var floatingLabelFont: UIFont = UIFont.systemFont(ofSize: 14) {
**didSet** {  
    **self**.floatingLabel.font = **self**.floatingLabelFont  
    **self**.font = **self**.floatingLabelFont  
    **self**.setNeedsDisplay()  
}  
}

Second, Our initializer. copy and paste this just below the last variable

required init?(coder aDecoder: NSCoder) {

**super**.init(coder: aDecoder)  
**self**._placeholder = (**self**._placeholder != **nil**) ? **self**._placeholder : placeholder // Use our custom placeholder if none is set  
placeholder = self._placeholder // make sure the placeholder is shown **self**.floatingLabel = UILabel(frame: CGRect.zero) **self**.addTarget(**self**, action: **
# selector*(*self.addFloatingLabel), for: .editingDidBegin)
**self**.addTarget(**self**, action: **
# selector*(*self.removeFloatingLabel), for: .editingDidEnd)}

As we can see from the code above, we attach a listener to our text field to fire when the user focuses on a text field and when the cursor is removed. now we just need the logic to add/remove our floating label. Please add the code below:

// Add a floating label to the view on becoming first responder*@objc* func addFloatingLabel() {

**if** **self**.text == "" {  
    **self**.floatingLabel.textColor = floatingLabelColor  
    **self**.floatingLabel.font = floatingLabelFont  
    **self**.floatingLabel.text = **self**._placeholder  
    **self**.floatingLabel.layer.backgroundColor = UIColor.white.cgColor  
    **self**.floatingLabel.translatesAutoresizingMaskIntoConstraints = **false** 
self.floatingLabel.clipsToBounds = true self.floatingLabel.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.floatingLabelHeight)self.layer.borderColor = self.activeBorderColor.cgColor
    **self**.addSubview(**self**.floatingLabel)        
    **self**.floatingLabel.bottomAnchor.constraint(equalTo:  
    **self**.topAnchor, constant: -10).isActive = true _// Place our label 10pts above the text field_        // Remove the placeholder  
    **self**.placeholder = ""  
}  
**self**.setNeedsDisplay()  
}

What the code above does is quite simple, set a text for our label, add it as a subview to our input field and activate layout constraints to position it properly on the screen, then remove the placeholder. we used UIView.animate to achieve a simple slide up animation. Next, we need to be able to remove our label. add the code below

@objc func removeFloatingLabel() {

**if** **self**.text == "" {  
    UIView.animate(withDuration: 0.13) {  
       **self**.subviews.forEach{ $0.removeFromSuperview() }  
       **self**.setNeedsDisplay()  
    }  
    **self**.placeholder = **self**._placeholder  
}  
**self**.layer.borderColor = UIColor.black.cgColor  
}

Step 2: We create our text field in our storyboard and assign it’s class to our custom `UITextField` class like below

Next, we connect it to an IBOutlet by dragging from our storyboard to our while holding our ViewController like below

We are done. now we have a custom floating label style input field. lets see a small demo of our code in action