acani / CodeInputView

4-Digit Code Input Text Field
39 stars 9 forks source link

How to add Dots instead of showing number ? #1

Open MithunReddy opened 8 years ago

ma11hew28 commented 8 years ago

You can't. Well, at least not without editing the source code. I.e., there's no property you can set, like the secureTextEntry property of UITextInputTraits. Although, that would be a cool option and should be fairly easy to add. See if you can figure it out. ;-)

See also https://github.com/venmo/VENTouchLock.

MithunReddy commented 8 years ago

how to set "secureTextEntry" in swift can u help me in that. i tried below code and "var secureTextEntry: Bool { get{ return true} set {} }" and got an error screen shot 2016-01-13 at 7 36 22 pm

ma11hew28 commented 8 years ago

Hmm... Try asking on Stack Overflow.

ma11hew28 commented 8 years ago

Here's the basic idea.

Bonus

Note: Don't make CodeInputView conform to UITextInputTraits.

chandanvarma009 commented 8 years ago

import UIKit

class CodeInputView: UIView, UIKeyInput { var delegate: CodeInputViewDelegate? var nextTag = 1 var secureCode = "" var secure:Bool = false // MARK: - UIResponder

override func canBecomeFirstResponder() -> Bool {
    return true
}

// MARK: - UIView

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

    // Add four digitLabels
    var frame = CGRect(x: 5, y: 0, width: 55, height: 55)
    for index in 1...4 {
        let digitLabel = UILabel(frame: frame)
        digitLabel.font = UIFont.systemFontOfSize(42)
        digitLabel.backgroundColor = UIColor.yiWhiteColor()
        digitLabel.tag = index
        digitLabel.text = " "
        digitLabel.textAlignment = .Center
        self.addSubview(digitLabel)
        frame.origin.x += 55 + 10
    }
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

// MARK: - UIKeyInput

func hasText() -> Bool {
    return nextTag > 1 ? true : false
}

func insertText(text: String) {

    if nextTag < 5 {
        (self.viewWithTag(nextTag) as! UILabel).text = text

        if (nextTag == 1){
         secureCode = (self.viewWithTag(nextTag) as! UILabel).text!
        } else {
         secureCode += (self.viewWithTag(nextTag) as! UILabel).text!
        }
        if secure == true {
            (self.viewWithTag(nextTag) as! UILabel).text! = "\u{2022}"
        }

        nextTag += 1

        if nextTag == 5 {

            delegate?.codeInputView(self, didFinishWithCode: secureCode)

        }
    }
}

func deleteBackward() {
    if nextTag > 1 {
        nextTag -= 1
        (self.viewWithTag(nextTag) as! UILabel).text = " "

        secureCode = String(secureCode.characters.dropLast())
    }
}

func clear() {
    while nextTag > 1 {
        deleteBackward()
    }
}

// MARK: - UITextInputTraits

var keyboardType: UIKeyboardType { get { return .NumberPad } set { } }

}

protocol CodeInputViewDelegate { func codeInputView(codeInputView: CodeInputView, didFinishWithCode code: String) }

chandanvarma009 commented 8 years ago

Use it as codeInputView?.secure = true will display your text securely as dot i.e. \u{2022}

MithunReddy commented 8 years ago

That works !!! Thanks @chandanvarma009

aditya-capsitech commented 3 years ago

how to set auto fill otp in this??