DTTextField is a custom textfield with floating placeholder and error label

Overview

DTTextField

Version License Awesome Platform Xcode

Demo

Introduction

DTTextField is a UITextField library with floating placeholder and error label.

Floating placeholder inspired from JVFloatLabeledTextField 👍 .

Example

Clone the repo and run the example project from the Example directory.

Requirements

  • Xcode 11.0+
  • Swift 5.0+

Installation

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

pod 'DTTextField'

Usage

  1. Open a storyboard or Xib file.
  2. Drag and drop a UITextField to a ViewController.
  3. In Identity Inspector, replace the class from UITextField to DTTextField and the module to DTTextField.

Properties

Property name Type Remark
errorMessage String Add your error message to this property
errorFont UIFont Change font of error text
paddingYErrorLabel CGFloat Error text top padding
floatPlaceholderColor UIColor To change float placeholder color
floatPlaceholderActiveColor UIColor To change float placeholder color while TextField is active(First responder)
floatPlaceholderFont UIFont Change font of float placeholder
paddingYFloatLabel CGFloat float placeholder top padding
placeholderColor UIColor change placeholder color
animateFloatPlaceholder Bool animate float placeholder label
hideErrorWhenEditing Bool hide error label when typing
floatingDisplayStatus enum maintain display status always, never, defaults

Important Properties

Property name Type Remark
dtLayer CALayer If you want to formate DTTextField than use dtLayer property instead of layer (e.g. background color)
borderColor UIColor Change border color of DTTextField
canShowBorder Bool Toggle border of DTTextField
dtborderStyle enum none, rounded , sqare , top , bottom , left , right

Methods

Method Name Remark
showError to show error message
hideError to hide error message

Note

Don't give height constraint to DTTextField otherwise it won't work properly.

TODO

  • Add inbuilt validation

Author

Dhaval Thanki 😎

License

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

Comments
  • DTTextField Crash

    DTTextField Crash

    Hi!

    Some users are getting this error when using DTTextField;

    Crashed: com.apple.main-thread 0 DTTextField 0x104209498 specialized DTTextField.insetRectForBounds(rect : CGRect) -> CGRect (DTTextField.swift:285) 1 DTTextField 0x104206324 @objc DTTextField.textRect(forBounds : CGRect) -> CGRect + 344 2 UIKit 0x18cfec0fc -[UITextField _marginTopForBounds:] + 184 3 UIKit 0x18c428c70 -[UITextField _originForTextFieldLabel:] + 128 4 UIKit 0x18c4209f4 -[UIFieldEditor textContainerOrigin] + 212 5 UIFoundation 0x1a11c27b0 -[NSLayoutManager _primitiveInvalidateDisplayForGlyphRange:] + 2836 6 UIFoundation 0x1a11c2f34 -[NSLayoutManager invalidateDisplayForCharacterRange:] + 436 7 UIFoundation 0x1a119cbc0 -[NSLayoutManager(NSPrivate) _setExtraLineFragmentRect:usedRect:textContainer:] + 416 8 UIFoundation 0x1a11f952c -[NSTypesetter _layoutGlyphsInLayoutManager:startingAtGlyphIndex:maxNumberOfLineFragments:maxCharacterIndex:nextGlyphIndex:nextCharacterIndex:] + 6060 9 UIFoundation 0x1a11a0c94 -[NSATSTypesetter layoutGlyphsInLayoutManager:startingAtGlyphIndex:maxNumberOfLineFragments:nextGlyphIndex:] + 364 10 UIFoundation 0x1a11bfc28 -[NSLayoutManager setTextStorage:] + 720 11 UIFoundation 0x1a11e97a8 -[NSTextStorage addLayoutManager:] + 100 12 UIKit 0x18c755040 -[UIFieldEditor initWithTextField:textStorage:] + 752 13 UIKit 0x18c429b38 -[UITextField _fieldEditor] + 196 14 UIKit 0x18cff6700 -[UITextField _initContentView] + 60 15 UIKit 0x18cfe928c -[UITextField initWithCoder:] + 1680 16 DTTextField 0x10420898c specialized DTTextField.init(coder : NSCoder) -> DTTextField? (DTTextField.swift:166) 17 DTTextField 0x1042050e4 @objc DTTextField.init(coder : NSCoder) -> DTTextField? (DTTextField.swift) 18 UIKit 0x18c963fb4 -[UIClassSwapper initWithCoder:] + 248 19 UIKit 0x18cb01d24 UINibDecoderDecodeObjectForValue + 680 20 UIKit 0x18cb01a64 -[UINibDecoder decodeObjectForKey:] + 104 21 UIKit 0x18c963c24 -[UIRuntimeConnection initWithCoder:] + 136 22 UIKit 0x18c9643f8 -[UIRuntimeEventConnection initWithCoder:] + 68 23 UIKit 0x18cb01d24 UINibDecoderDecodeObjectForValue + 680 24 UIKit 0x18cb01e9c UINibDecoderDecodeObjectForValue + 1056 25 UIKit 0x18cb01a64 -[UINibDecoder decodeObjectForKey:] + 104 26 UIKit 0x18c962f98 -[UINib instantiateWithOwner:options:] + 1168 27 UIKit 0x18c7632cc -[UIViewController _loadViewFromNibNamed:bundle:] + 372 28 UIKit 0x18c525dd4 -[UIViewController loadView] + 176 29 UIKit 0x18c4f6ca8 -[UITableViewController loadView] + 92 30 UIKit 0x18c406dd8 -[UIViewController loadViewIfRequired] + 184 31 UIKit 0x18c4af870 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 76 32 UIKit 0x18c4aed00 -[UINavigationController _startTransition:fromViewController:toViewController:] + 196 33 UIKit 0x18c4ae6e8 -[UINavigationController _startDeferredTransitionIfNeeded:] + 876 34 UIKit 0x18c4ae254 -[UINavigationController __viewWillLayoutSubviews] + 156 35 UIKit 0x18c4ae15c -[UILayoutContainerView layoutSubviews] + 188 36 UIKit 0x18c4044f0 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1224 37 QuartzCore 0x18b920a84 -[CALayer layoutSublayers] + 148 38 QuartzCore 0x18b914d70 CA::Layer::layout_if_needed(CA::Transaction*) + 296 39 QuartzCore 0x18b914c2c CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32 40 QuartzCore 0x18b88b88c CA::Context::commit_transaction(CA::Transaction*) + 276 41 QuartzCore 0x18b8b2ae4 CA::Transaction::commit() + 520 42 QuartzCore 0x18b8b361c CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 92 43 CoreFoundation 0x18558c6e4 CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 32 44 CoreFoundation 0x18558a18c __CFRunLoopDoObservers + 412 45 CoreFoundation 0x18558a670 __CFRunLoopRun + 1076 46 CoreFoundation 0x1854a503c CFRunLoopRunSpecific + 436 47 GraphicsServices 0x195cfaf94 GSEventRunModal + 100 48 UIKit 0x18c46a86c UIApplicationMain + 208 49 0x103e7db80 main (LoginViewController.swift:16) 50 libdyld.dylib 0x1a8c37d1c start + 4

    Does someone have any ideas why there's this crash?

    Thanks! W

    opened by WilliamBlais 4
  • textFieldShouldReturn not working

    textFieldShouldReturn not working

    How can I implement textFieldShouldReturn when i press return button on keyboard?

    func textFieldShouldReturn(textField: UITextField) -> Bool { return true }

    Help me please. This is not calling...

    opened by pavelbowie 3
  • NSAttributedStringKey doesn't work

    NSAttributedStringKey doesn't work

    public var placeholderColor:UIColor?{ didSet{ guard let color = placeholderColor else { return } attributedPlaceholder = NSAttributedString(string: placeholderFinal, attributes: [NSAttributedStringKey.foregroundColor:color]) } }

    Type 'NSAttributedStringKey' (aka 'NSString') has no member 'foregroundColor'

    how to fix it?

    opened by petrovRV 3
  • Issue with Textfield background colour

    Issue with Textfield background colour

    How to change the background colour of Ui textfield , If i changed the background colour from Xcode then there is issue with the background colour of the textfield . After changing the colour it still showing white background colour. Tried Tint colour also but nothing works.

    opened by Ravinder-webonise 2
  • Feature Request: Option to change error label text color

    Feature Request: Option to change error label text color

    It would be useful to allow to change the text color of the error message. Would propose a new errorTextColor property similar to the existing errorFont property.

    opened by joaomvfsantos 2
  • Xcode 9 swift 4 crash iOS 11

    Xcode 9 swift 4 crash iOS 11

    DTTextField_Example[5003:262200] [Accessibility] ****************** Loading GAX Client Bundle **************** fatal error: unexpectedly found nil while unwrapping an Optional value 2017-09-21 17:44:22.977669-0500 DTTextField_Example[5003:262200] fatal error: unexpectedly found nil while unwrapping an Optional value

    opened by jsalazar78 2
  • Example project should not require pod install (broken)

    Example project should not require pod install (broken)

    Hi Dhaval

    Perhaps change the example project so that it doesn't require running pod install

    I downloaded the project, but couldn't get it working out of the box. Pod install didn't work for me.

    PROMPT> pod install
    Analyzing dependencies
    Downloading dependencies
    Installing DTTextField (0.2.5)
    
    [!] Error installing DTTextField
    [!] /usr/bin/git clone https://github.com/iDhaval/DTTextField.git /var/folders/54/gf7lcj014z7558cgt81dhks40000gn/T/d20170523-92356-1cqondp --template= --single-branch --depth 1 --branch 0.2.5
    
    Cloning into '/var/folders/54/gf7lcj014z7558cgt81dhks40000gn/T/d20170523-92356-1cqondp'...
    fatal: unable to access 'https://github.com/iDhaval/DTTextField.git/': Unknown SSL protocol error in connection to github.com:-36
    
    
    [!] Automatically assigning platform ios with version 9.0 on target DTTextField_Example because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
    PROMPT> 
    

    Environment

       CocoaPods : 1.2.1
            Ruby : ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
        RubyGems : 2.5.1
            Host : Mac OS X 10.12.4 (16E195)
           Xcode : 8.3.1 (8E1000a)
             Git : git version 2.11.0 (Apple Git-81)
    Ruby lib dir : /Users/johndoe/.rvm/rubies/ruby-2.3.0/lib
    Repositories : master - https://github.com/CocoaPods/Specs.git @ 8fd040a59f38bc862a98faadeb632a837cc5ef60
    
    opened by neoneye 2
  • Issue: input  mobile number only 9 digit.

    Issue: input mobile number only 9 digit.

    can You check in your demo mobile number input. I am adding your pod file and validate with number.

    `class func verifyPhoneNumber(text : String) -> (String, Bool) { var verifyObj = (message : "", isVerified : false) if text.length == 0 { verifyObj.message = vcErrorPhoneNumber

            //        } else if text.length < 10 { //< 8 { if country code option is also on screen
            //            verifyObj.message = vcErrorValidPhoneNumber
            
        }
    

    // else if text.length > 10{ // verifyObj.message = contactMayNOtBeGreaterThan // // } else if !text.isContainsAllZeros() { verifyObj.message = vcErrorValidPhoneNumber print("isContainsAllZeros",verifyObj)

        } else if !text.containsNumberOnly() {
            print("text mob number:- ",text)
            verifyObj.message = vcErrorValidPhoneNumber
            print("containsNumberOnly",verifyObj)
    

    // } else if !text.isValidMobileNumber() && text.count < 9 { verifyObj.message = vcErrorValidPhoneNumber print("text mob:- ",text) print(verifyObj)

        }
        else {
            verifyObj.isVerified = true
        }
        return verifyObj
    }`
    

    `func isContainsAllZeros() -> Bool { let mobileNoRegEx = "^0*$"; let mobileNoTest = NSPredicate(format:"SELF MATCHES %@", mobileNoRegEx) return mobileNoTest.evaluate(with: self) } func containsNumberOnly() -> Bool { let nameRegEx = "^[0-9]+$" let nameTest = NSPredicate(format:"SELF MATCHES %@", nameRegEx) let test = nameTest.evaluate(with: self) print(test) return test }

    func isValidMobileNumber() -> Bool { let mobileNoRegEx = "^[0-9]{8,16}$" // let mobileNoRegEx = "^\d{3}-\d{3}-\d{4}$" // let mobileNoRegEx = "^[0-9+]{0,1}+[0-9]{5,16}$" // // let mobileNoRegEx = "(?:(?:\+|0{0,2})91(\s*[\- ]\s*)?|[0 ]?)?[789]\d{9}|(\d[ -]?){10}\d"

        let mobileNoTest = NSPredicate(format:"SELF MATCHES %@", mobileNoRegEx)
    let texts = mobileNoTest.evaluate(with: self)
    print("Number",texts)
        return texts
    }`
    
    opened by Chandanjee 1
  • The text is positioned above the top line using the dtlayer property. Using Xcode 10 and Swift 4.2

    The text is positioned above the top line using the dtlayer property. Using Xcode 10 and Swift 4.2

    The text is positioned above the top line using the dtlayer property, how can it be solved?

    Before Write: image1

    After Write: image2

    Without using the dtlayer property: image3

    opened by jaag12 1
  • changing border style to none and customize field.

    changing border style to none and customize field.

    Hi, Thanks for the awesome control. Can you please tell how can I make it border to .none and add underline? I tried changing border style to none but seems its been used inside core logic. Kindly let me know. Thanks

    Regards, Amir

    opened by amirpervaiz086 1
  • NSForegroundColorAttributeName

    NSForegroundColorAttributeName

    we are facing issue when using pod of DTTextField. when we are trying to set "placeholder" Foreground color attribute then following compiler error occured (using xcode 9.1)

    'NSForegroundColorAttributeName' was obsoleted in Swift 3 (UIKit.NSForegroundColorAttributeName)

    opened by jitesh-sharma 1
  • Issue with Textfield input text.. on iphoneX , iphone 12 series , working fine on iphone 11

    Issue with Textfield input text.. on iphoneX , iphone 12 series , working fine on iphone 11

    Facing issue on selected iphones , x ,xs ,xs max , iphone 12 series phones. Working fine iphone 11 and all. For your help i am attaching screenshots of the textfield of iphone 11 and iphnoe 12 wth same code. Screenshot 2021-11-24 at 7 16 45 PM

    opened by Ravinder-webonise 1
Releases(0.7.0)
Owner
Dhaval Thanki
Enthusiastic self-taught iOS Developer
Dhaval Thanki
Floating-textfield-swiftui - Floating textfield With SwiftUI

floating_textfield-swiftui Hey, Guys welcome to this tutorial. In this complete

null 1 Feb 11, 2022
TTextField is developed to help developers can initiate a fully standard textfield including title, placeholder and error message in fast and convinient way without having to write many lines of codes

TTextField is developed to help developers can initiate a fully standard textfield including title, placeholder and error message in fast and convinient way without having to write many lines of codes

Nguyen Duc Thinh 7 Aug 28, 2022
Floating Label TextField for SwiftUI. FloatingLabelTextFieldSwiftUI

FloatingLabelTextFieldSwiftUI FloatingLabelTextFieldSwiftUI is a small and lightweight SwiftUI framework written in completely swiftUI (not using UIVi

Kishan Raja 337 Jan 2, 2023
A floating label style for SwiftUI's TextField.

FloatingLabelTextFieldStyle A floating label style for TextField with support for displaying error messages. Requirements iOS 15.0+ macOS 12.0+ Instal

Red Davis 13 Aug 22, 2022
A SwiftUI TextField with a prompt (or placeholder) that floats above the text field when active or not empty. Requires iOS 15.

FloatingPromptTextField A prompt is the label in a text field that informs the user about the kind of content the text field expects. In a default Tex

Emilio Peláez 43 Nov 3, 2022
Simple placeholder move textfield

PlaceholderTextField Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements iOS 11.0 o

null 6 Mar 9, 2022
Placeholder views based on content, loading, error or empty states

StatefulViewController A protocol to enable UIViewControllers or UIViews to present placeholder views based on content, loading, error or empty states

Alexander Schuch 2.1k Dec 8, 2022
This is the demo repository for having floating TextField in swift UI

ed-floating-field-swiftui This is the demo repository for having floating TextField in swift UI The inputs fields should not be empty Validations for

Patrick 1 May 26, 2022
A custom TextField with a switchable icon which shows or hides the password and enforce good password policies

PasswordTextField A custom TextField with a switchable icon which shows or hides the password and enforces good password policies, written in Swift. ⭐

Chris Jimenez 304 Dec 29, 2022
An UITextView in Swift. Support auto growing, placeholder and length limit.

GrowingTextView Requirements iOS 8.0 or above Installation CocoaPods GrowingTextView is available through CocoaPods. To install it, simply add the fol

Kenneth Tsang 941 Jan 5, 2023
HTYTextField A UITextField with bouncy placeholder.

HTYTextField - A UITextField with bouncy placeholder. Screenshot Installation CocoaPods Add the dependency to your Podfile

Hanton Yang 312 Nov 13, 2022
A light-weight UITextView subclass that adds support for placeholder.

RSKPlaceholderTextView A light-weight UITextView subclass that adds support for placeholder. Installation Using Swift Package Manager To add the RSKPl

Ruslan Skorb 220 Dec 17, 2022
A UITextView subclass that adds support for multiline placeholder written in Swift.

KMPlaceholderTextView A UITextView subclass that adds support for multiline placeholder written in Swift. Usage You can set the value of the placehold

Zhouqi Mo 795 Nov 18, 2022
DGPlaceholderTextView - A light-weight UITextView that supports for placeholder

DGPlaceholderTextView Requirements Installation Usage Properties DGPlaceholderTe

donggyu 5 Jan 26, 2022
Awesome TextField is a nice and simple libriary for iOS and Mac OSX

Awesome TextField is a nice and simple libriary for iOS and Mac OSX. It's highly customisable and easy-to-use tool. Works perfectly for any registration or login forms in your app.

Alexander Shoshiashvili 225 Nov 21, 2022
Elegant SwiftUI phone number textField.

iPhoneNumberField ☎️ Format phone numbers as they're typed—entirely in SwiftUI. ?? Get Started | Examples | Customize | Features | Install | And it's

Seyed Mojtaba Hosseini Zeidabadi 358 Dec 30, 2022
An optional TextField Replacement for SwiftUI

TextInputView A TextField Replacement In his excellent YouTube tutorial series on Building SwiftUI Components, Peter Friese (@peterfriese on Twitter)

Stewart Lynch 6 Aug 9, 2022
TextField with DropDown support using UIPickerView

IQDropDownTextField TextField with DropDown support using UIPickerView Installing Install using cocoapods. Add in your Podfile: pod 'IQDropDownTextFie

Mohd Iftekhar Qurashi 301 Jun 29, 2022
TextField with smart suggestion

AutoCompleteTextField Features Provides a subclass of UITextField that has suggestion from input Has autocomplete input feature Data suggestion are pr

Neil Francis Ramirez Hipona 63 Nov 13, 2022