如何将后续文本添加到uitextfield

如何将后续文本添加到uitextfield

本文介绍了如何将后续文本添加到uitextfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有占位符的文本字段,名为 letschat 。现在每当我开始输入我的文本字段时,我想将我的文本字段显示为某些@letschat 。当我的文本字段为空时,我的占位符必须显示。我做了。但是每当我开始在文本字段中输入时,我想设置。无论我输入什么,我希望这个文本也可见:

I have one text field with place holder called letschat. Now whenever I start typing in my textfield, I want to show my textfield as some @letschat. When my textfield is empty that time my placeholder have to show. That I did. But I want to set whenever I start typing in my textfield. Whatever I am typing with that I want this text also to visible like:

我该怎么做?

推荐答案

我创建了 UITextField 使用占位符(如果设置)作为后缀的子类。据我所知,一切都按预期工作。也许需要进行一些调整以满足您的需求。

I created a UITextField subclass that uses the placeholder (if set) as a suffix. As far as I can see everything works as expected. Maybe there are some tweaks needed to suit your needs.

随意询问是否有任何不清楚的地方:

Feel free to ask if anything is unclear:

class SuffixTextField: UITextField {

    override init(frame: CGRect) {
        super.init(frame: frame)
        sharedInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        sharedInit()
    }

    private func sharedInit() {
        addTarget(self, action: #selector(textChanged), for: .editingChanged)
    }

    override var text: String? {
        didSet {
            selectedTextRange = maxTextRange
        }
    }

    override var attributedText: NSAttributedString? {
        didSet {
            selectedTextRange = maxTextRange
        }
    }

    @objc private func textChanged() {
        if let currentText = text, let placeholder = placeholder {
            if currentText == placeholder {
                self.text = nil
            } else if !currentText.hasSuffix(placeholder) {
                self.text = currentText + placeholder
            }
        }
    }

    private var maxCursorPosition: UITextPosition? {
        guard let placeholder = placeholder, !placeholder.isEmpty else { return nil }
        guard let text = text, !text.isEmpty else { return nil }
        return position(from: beginningOfDocument, offset: (text as NSString).range(of: placeholder, options: .backwards).location)
    }

    private var maxTextRange: UITextRange? {
        guard let maxCursorPosition = maxCursorPosition else { return nil }
        return textRange(from: maxCursorPosition, to: maxCursorPosition)
    }

    override var selectedTextRange: UITextRange? {
        get { return super.selectedTextRange }
        set {
            guard let newRange = newValue,
                let maxCursorPosition = maxCursorPosition else {
                    super.selectedTextRange = newValue
                    return
            }

            if compare(maxCursorPosition, to: newRange.start) == .orderedAscending {
                super.selectedTextRange = textRange(from: maxCursorPosition, to: maxCursorPosition)
            } else if compare(maxCursorPosition, to: newRange.end) == .orderedAscending {
                super.selectedTextRange = textRange(from: newRange.start, to: maxCursorPosition)
            } else {
                super.selectedTextRange = newValue
            }
        }
    }

}

在这里你可以看到预览:

here you can see a preview:https://www.dropbox.com/s/etkbme37wuxbw1q/preview.mov?dl=0

这篇关于如何将后续文本添加到uitextfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:34