如何实现占位符文本在UITextField中逐个字符消失

Vis*_*l16 20 xcode objective-c placeholder uitextfield ios

你能帮我么.

UITextField我们提供了一个占位符的文本,当我们输入任何字符的字符串占位符将会消失.我怎样才能实现只输入的字符不会是一个完整的字符串?这意味着如果我键入3个字符,只占位符的前3个字符将消失.

在此输入图像描述

#EDIT 1

此外,新输入的字符文本颜色将更改,其他剩余字符文本颜色保持不变.

提前致谢.

BJH*_*ios 5

我不认为占位符的默认行为是可编辑的,但是您想要完成的任务可以使用NSAttributedString模拟占位符值来完成。

我确信这可以优化,但在这里我创建了一个处理程序类,它充当给定的委托UITextField,操纵用户输入的字符串以达到所需的效果。您可以使用所需的占位符字符串初始化处理程序,这样您就可以使任何文本字段以这种方式工作。

自定义占位符文本字段

import UIKit

class CustomPlaceholderTextFieldHandler: NSObject {
    let placeholderText: String
    let placeholderAttributes = [NSForegroundColorAttributeName : UIColor.lightGray]
    let inputAttributes = [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 153/255, blue: 0, alpha: 1.0)]
    var input = ""

    init(placeholder: String) {
        self.placeholderText = placeholder
        super.init()
    }

    func resetPlaceholder(for textField: UITextField) {
        input = ""
        setCombinedText(for: textField)
    }

    fileprivate func setCursorPosition(for textField: UITextField) {
        guard let cursorPosition = textField.position(from: textField.beginningOfDocument, offset: input.characters.count)
            else { return }

        textField.selectedTextRange = textField.textRange(from: cursorPosition, to: cursorPosition)
    }

    fileprivate func setCombinedText(for textField: UITextField) {
        let placeholderSubstring = placeholderText.substring(from: input.endIndex)
        let attributedString = NSMutableAttributedString(string: input + placeholderSubstring, attributes: placeholderAttributes)
        attributedString.addAttributes(inputAttributes, range: NSMakeRange(0, input.characters.count))
        textField.attributedText = attributedString
    }
}

extension CustomPlaceholderTextFieldHandler: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        if string == "" {
            if input.characters.count > 0 {
                input = input.substring(to: input.index(before: input.endIndex))
            }
        } else {
            input += string
        }

        if input.characters.count <= placeholderText.characters.count {
            setCombinedText(for: textField)
            setCursorPosition(for: textField)
            return false
        }
        return true
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        setCursorPosition(for: textField)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我初始化上面的 gif 的方法:

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!
    let placeholderHandler = CustomPlaceholderTextFieldHandler(placeholder: "_2_-__-__A")

    override func viewDidLoad() {
        super.viewDidLoad()
        textField.delegate = placeholderHandler
        placeholderHandler.resetPlaceholder(for: textField)
    }
}
Run Code Online (Sandbox Code Playgroud)

这可以扩展为在初始化时获取颜色参数、字体等,或者您可能会发现子类化UITextField并使其成为自己的委托更干净。我还没有真正测试过选择/删除/替换多个字符。

input变量将返回用户在任何给定点输入的文本。此外,使用固定宽度字体可以消除用户键入时的抖动并替换占位符文本。