【问题标题】:PlaceHolder animates when start typing in TextField in iOS在 iOS 中开始输入 TextField 时 PlaceHolder 动画
【发布时间】:2016-05-04 06:59:27
【问题描述】:

如何在UITextField中设置这种动画?如今,许多应用程序都在使用它。

【问题讨论】:

  • 您是否需要在文本字段获得焦点时运行(酷)动画,或者当您想从最后一个字段移动焦点时运行?
  • 是的,我想要那个......或者类似的东西......如今,许多应用程序都在使用它......
  • 您可以使用另一个 uilabel 或 uitextfield 来完成此任务,为此您需要做的是,您只需在其上添加 uilabel 或 utextfiel 并将其留空表单界面构建器,然后在方法上设置文本值您开始在 uitextfield 中输入的位置。

标签: ios objective-c swift uitextfield placeholder


【解决方案1】:

我找到了解决方案。您可以使用多个标签管理此类动画,并将这些标签显示-隐藏到 textFieldDidBeginEditing 方法中。

如果您想要与您在问题中描述的相同的漂亮动画,请尝试关注UITextField 的第三方存储库。

如果您正在寻找与此动画等效的 UITextView,请访问 UIFloatLabelTextView 存储库。

【讨论】:

  • UIFloatLableTextField 帮助了我。谢谢!
【解决方案2】:

对于 Swift 4.0 和 4.2

检查这个库的浮动文本字段

https://github.com/hasnine/iOSUtilitiesSource

代码:

enum placeholderDirection: String {
    case placeholderUp = "up"
    case placeholderDown = "down"

}
public class IuFloatingTextFiledPlaceHolder: UITextField {
    var enableMaterialPlaceHolder : Bool = true
    var placeholderAttributes = NSDictionary()
    var lblPlaceHolder = UILabel()
    var defaultFont = UIFont()
    var difference: CGFloat = 22.0
    var directionMaterial = placeholderDirection.placeholderUp
    var isUnderLineAvailabe : Bool = true
    override init(frame: CGRect) {
        super.init(frame: frame)
        Initialize ()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        Initialize ()
    }
    func Initialize(){
        self.clipsToBounds = false
        self.addTarget(self, action: #selector(IuFloatingTextFiledPlaceHolder.textFieldDidChange), for: .editingChanged)
        self.EnableMaterialPlaceHolder(enableMaterialPlaceHolder: true)
        if isUnderLineAvailabe {
            let underLine = UIImageView()
            underLine.backgroundColor = UIColor.init(red: 197/255.0, green: 197/255.0, blue: 197/255.0, alpha: 0.8)
            //            underLine.frame = CGRectMake(0, self.frame.size.height-1, self.frame.size.width, 1)
            underLine.frame = CGRect(x: 0, y: self.frame.size.height-1, width : self.frame.size.width, height : 1)

            underLine.clipsToBounds = true
            self.addSubview(underLine)
        }
        defaultFont = self.font!

    }
    @IBInspectable var placeHolderColor: UIColor? = UIColor.lightGray {
        didSet {
            self.attributedPlaceholder = NSAttributedString(string: self.placeholder! as String ,
                                                            attributes:[NSAttributedString.Key.foregroundColor: placeHolderColor!])
        }
    }
    override public var placeholder:String?  {
        didSet {
            //  NSLog("placeholder = \(placeholder)")
        }
        willSet {
            let atts  = [NSAttributedString.Key.foregroundColor.rawValue: UIColor.lightGray, NSAttributedString.Key.font: UIFont.labelFontSize] as! [NSAttributedString.Key : Any]
            self.attributedPlaceholder = NSAttributedString(string: newValue!, attributes:atts)
            self.EnableMaterialPlaceHolder(enableMaterialPlaceHolder: self.enableMaterialPlaceHolder)
        }

    }
    override public var attributedText:NSAttributedString?  {
        didSet {
            //  NSLog("text = \(text)")
        }
        willSet {
            if (self.placeholder != nil) && (self.text != "")
            {
                let string = NSString(string : self.placeholder!)
                self.placeholderText(string)
            }

        }
    }
    @objc func textFieldDidChange(){
        if self.enableMaterialPlaceHolder {
            if (self.text == nil) || (self.text?.count)! > 0 {
                self.lblPlaceHolder.alpha = 1
                self.attributedPlaceholder = nil
                self.lblPlaceHolder.textColor = self.placeHolderColor
                self.lblPlaceHolder.frame.origin.x = 0 ////\\
                let fontSize = self.font!.pointSize;
                self.lblPlaceHolder.font = UIFont.init(name: (self.font?.fontName)!, size: fontSize-3)
            }
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1, options: .curveEaseInOut, animations: {() -> Void in
                if (self.text == nil) || (self.text?.count)! <= 0 {
                    self.lblPlaceHolder.font = self.defaultFont
                    self.lblPlaceHolder.frame = CGRect(x: self.lblPlaceHolder.frame.origin.x+10, y : 0, width :self.frame.size.width, height : self.frame.size.height)
                }
                else {
                    if self.directionMaterial == placeholderDirection.placeholderUp {
                        self.lblPlaceHolder.frame = CGRect(x : self.lblPlaceHolder.frame.origin.x, y : -self.difference, width : self.frame.size.width, height : self.frame.size.height)
                    }else{
                        self.lblPlaceHolder.frame = CGRect(x : self.lblPlaceHolder.frame.origin.x, y : self.difference, width : self.frame.size.width, height : self.frame.size.height)
                    }

                }
            }, completion: {(finished: Bool) -> Void in
            })
        }
    }
    func EnableMaterialPlaceHolder(enableMaterialPlaceHolder: Bool){
        self.enableMaterialPlaceHolder = enableMaterialPlaceHolder
        self.lblPlaceHolder = UILabel()
        self.lblPlaceHolder.frame = CGRect(x: 0, y : 0, width : 0, height :self.frame.size.height)
        self.lblPlaceHolder.font = UIFont.systemFont(ofSize: 10)
        self.lblPlaceHolder.alpha = 0
        self.lblPlaceHolder.clipsToBounds = true
        self.addSubview(self.lblPlaceHolder)
        self.lblPlaceHolder.attributedText = self.attributedPlaceholder
        //self.lblPlaceHolder.sizeToFit()
    }
    func placeholderText(_ placeholder: NSString){
        let atts  = [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: UIFont.labelFontSize] as [NSAttributedString.Key : Any]
        self.attributedPlaceholder = NSAttributedString(string: placeholder as String , attributes:atts)
        self.EnableMaterialPlaceHolder(enableMaterialPlaceHolder: self.enableMaterialPlaceHolder)
    }
    override public func becomeFirstResponder()->(Bool){
        let returnValue = super.becomeFirstResponder()
        return returnValue
    }
    override public func resignFirstResponder()->(Bool){
        let returnValue = super.resignFirstResponder()
        return returnValue
    }

    override public func layoutSubviews() {
        super.layoutSubviews()
    }
}

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
  • @greg-449 这是巨大的代码,这就是为什么我建立一个库并给他这个链接来详细说明。所以可能是可以接受的。对吗?
【解决方案3】:

这个问题可以通过使用多个标签和文本字段从逻辑上解决,稍后我们可以根据需要添加动画。我将使用三个图像来解释这个问题,即 Img1、Img2 和 Img3。

Img1 指向我们设计界面的情节提要。这里我们使用了三个标签,每个标签后跟 TextField 和 UIView(Textfield 下方的行)。

Img2:当应用程序启动或按下底部的“注册”按钮时,它指向初始屏幕,重置屏幕。在此图像中,标签被隐藏,因为文本字段为空白且视图颜色为灰色。

Img3:此图像反映了 Textfield 的编辑。当我们开始编辑文本字段(这里是第一个,即名称)时,标签出现,文本字段的大小减小,占位符更改,视图颜色变为黑色。

我们还需要记住一件事,当我们停止编辑任何文本字段时,如果它仍然为空白,则将其属性设置为原始。

我正在为这个问题添加代码,我在面试中被要求作为作业。

import UIKit

class FloatingLabelViewController: UIViewController, UITextFieldDelegate, UINavigationControllerDelegate {


  //UITextFieldDelegate - protocol defines methods that you use to manage the editing and validation of text in a UITextField object. All of the methods of this protocol are optional.

  //UINavigationControllerDelegate - Use a navigation controller delegate (a custom object that implements this protocol) to modify behavior when a view controller is pushed or popped from the navigation stack of a UINavigationController object.

  @IBOutlet weak var NameLbl: UILabel!
  @IBOutlet weak var EmailLbl: UILabel!
  @IBOutlet weak var PasswordLbl: UILabel!

  @IBOutlet weak var NameTxf: UITextField!
  @IBOutlet weak var EmailTxf: UITextField!
  @IBOutlet weak var PasswordTxf: UITextField!

  @IBOutlet weak var SignUpBtn: UIButton!

  @IBOutlet weak var NameView: UIView!
  @IBOutlet weak var EmailView: UIView!
  @IBOutlet weak var PasswordView: UIView!


  override func viewDidLoad() {
    super.viewDidLoad()


    NameTxf.delegate = self
    EmailTxf.delegate = self
    PasswordTxf.delegate = self

    self.property()

    //black  is varaiable here
    //setTitleColor - Sets the color of the title to use for the specified state
    //var layer - The view’s Core Animation layer used for rendering. this propert is never nil
    //cg color - Quartz color refernce

    SignUpBtn.backgroundColor = UIColor.black
    SignUpBtn.setTitleColor(UIColor.white, for: .normal)
    SignUpBtn.layer.borderWidth = 1
    SignUpBtn.layer.borderColor = UIColor.black.cgColor

    //Tap guesture recognizer to hide keyboard
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(FloatingLabelViewController.dismissKeyboard))
    view.addGestureRecognizer(tap)

    // UITapGestureRecognizer - UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for single or multiple taps. For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.


  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

  }

  //textFieldShouldReturn - Asks the delegate if the text field should process the pressing of the return button. The text field calls this method whenever the user taps the return button.  YES if the text field should implement its default behavior for the return button; otherwise, NO.

  // endEditing - Causes the view (or one of its embedded text fields) to resign the first responder status.

  func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
  }


  func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
  }

  //When user Starts Editing the textfield
  // textFieldDidBeginEditing - Tells the delegate that editing began in the specified text field

  func textFieldDidBeginEditing(_ textField: UITextField) {

    if textField == self.NameTxf
    {

        self.NameTxf.font = UIFont.italicSystemFont(ofSize: 15)
        self.NameLbl.isHidden = false
        self.NameLbl.text = self.NameTxf.placeholder
        self.NameTxf.placeholder = "First Last"
        NameView.backgroundColor = UIColor.black.withAlphaComponent(0.5)


    }

    else if textField == self.EmailTxf
    {
        self.EmailTxf.font = UIFont.italicSystemFont(ofSize: 15)
        self.EmailLbl.isHidden = false
        self.EmailLbl.text = self.EmailTxf.placeholder
        self.EmailTxf.placeholder = "abc@gmail.com"
        EmailView.backgroundColor = UIColor.black.withAlphaComponent(0.5)


    }

    else if textField == self.PasswordTxf
    {
        self.PasswordTxf.font = UIFont.italicSystemFont(ofSize: 15)
        self.PasswordLbl.isHidden = false
        self.PasswordLbl.text = self.PasswordTxf.placeholder
        self.PasswordTxf.placeholder = "........."
        PasswordView.backgroundColor = UIColor.black.withAlphaComponent(0.5)


    }


  }


  //When User End editing the textfield.

  // textFieldDidEndEditing - Tells the delegate that editing stopped for the specified text field.

  func textFieldDidEndEditing(_ textField: UITextField) {


    //Checkes if textfield is empty or not after after user ends editing.
    if textField == self.NameTxf
    {
        if self.NameTxf.text == ""
        {
        self.NameTxf.font = UIFont.italicSystemFont(ofSize: 25)
        self.NameLbl.isHidden = true
        self.NameTxf.placeholder = "Name"
        NameView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)




    }


    }


    else if textField == self.EmailTxf
    {
        if self.EmailTxf.text == ""
        {
            self.EmailTxf.font = UIFont.italicSystemFont(ofSize: 25)
            self.EmailLbl.isHidden = true
            self.EmailTxf.placeholder = "Email"
            EmailView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)



        }


    }



    else if textField == self.PasswordTxf
    {
        if self.PasswordTxf.text == ""
        {
            self.PasswordTxf.font = UIFont.italicSystemFont(ofSize: 25)
            self.PasswordLbl.isHidden = true
            self.PasswordTxf.placeholder = "Password"
            PasswordView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)


        }


    }


  }

  //Action on SingUp button Clicked.
  @IBAction func signupClicked(_ sender: Any) {

      self.property()
      self.dismissKeyboard() //TO dismiss the Keyboard on the click of SIGNUP button.

  }


  //Function to set the property of Textfields, Views corresponding to TextFields and Labels.
  func property(){

    NameLbl.isHidden =  true
    EmailLbl.isHidden = true
    PasswordLbl.isHidden = true

    NameTxf.text = ""
    EmailTxf.text = ""
    PasswordTxf.text = ""

    NameTxf.placeholder = "Name"
    EmailTxf.placeholder = "Email"
    PasswordTxf.placeholder = "Password"


    self.NameTxf.font = UIFont.italicSystemFont(ofSize: 25)
    self.EmailTxf.font = UIFont.italicSystemFont(ofSize: 25)
    self.PasswordTxf.font = UIFont.italicSystemFont(ofSize: 25)

    EmailTxf.keyboardType = UIKeyboardType.emailAddress
    PasswordTxf.isSecureTextEntry = true
    NameTxf.autocorrectionType = .no
    EmailTxf.autocorrectionType = .no

    NameView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
    EmailView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
    PasswordView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)   
  }
}

【讨论】:

    【解决方案4】:

    您可以尝试使用JSInputField,它也支持数据验证。

    JSInputField *inputField = [[JSInputField alloc] initWithFrame:CGRectMake(10, 100, 300, 50)];
    [self.view addSubview:inputField];
    [inputField setPlaceholder:@"Enter Text"];
    [inputField setRoundedCorners:UIRectCornerAllCorners];
    [inputField addValidationRule:JSCreateRuleNotNullValue]; //This will validate field for null value. It will show error if field is empty.
    [inputField addValidationRule:JSCreateRuleNumeric(2)];  //This will validate field for numeric values and restrict to enter value upto 2 decimal places.
    

    【讨论】:

      【解决方案5】:

      您可以使用 SkyFloatingLabelTextField。它是 SkyScanner 的此类标签或文本字段动画库。

      https://github.com/Skyscanner/SkyFloatingLabelTextField

      我希望这个答案对你有用。

      享受吧。

      【讨论】:

        【解决方案6】:

        使用此代码

        [your_textfieldname setShowsTouchWhenHighlighted:YES];
        

        【讨论】:

        • 这是做什么的?我找不到它的文档,但听起来很有用!
        • 我在我的应用程序中使用了这段代码,它工作正常,就像在问题中提到的那样
        • 我很困惑 - 我在任何地方都找不到它的文档,我只是在一个 Objective-C 项目中尝试过它,但它无法为 UITextField 编译......它是否已被弃用?
        • 在 Swift 中没有这样的方法。我正在使用 Xcode 7.3.1。
        • 与问题完全无关,这只是突出按钮的触摸。
        猜你喜欢
        • 2021-05-18
        • 2017-08-26
        • 2017-12-04
        • 1970-01-01
        • 2018-03-19
        • 2021-03-08
        • 1970-01-01
        • 1970-01-01
        • 2021-08-19
        相关资源
        最近更新 更多