最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift中把secureTextEntry的textfield的密码输入框输入的内容从原点换成星号

iOS crifan 5399浏览 0评论

用代码:

            self.passwordTextField.secureTextEntry = true

要换成星号

swift textfiled secure point to start

swift textfiled secureTextEntry use star

ios – Secure UITextField text change to (*) asterisk character – Stack Overflow

说是自己实现EditingChanged:

self.passwordTextField.addTarget(self, action: #selector(self.textFiledEditingChanged(_:)), forControlEvents: UIControlEvents.EditingChanged)

但是也不是很好:把真正输入的内容换掉了。。

swift textfiled secureTextEntry asterisk

swift textfiled asterisk

How can I change the dots to another character on password field in iOS Swift – Stack Overflow

ios – UITextField secureTextEntry bullets with a custom font? – Stack Overflow

最后,代码写成:

//
//  PasswordTextField.swift
//  CrifanLibSwift
//
//  Created by licrifan on 16/7/8.
//  Copyright © 2016年 licrifan. All rights reserved.
//
import UIKit
class PasswordTextField: CommonTextField, UITextFieldDelegate {
    var realText:String {
        didSet {
            print(“self.text=\(self.text), realText=\(realText)”)
            updateMaskStr()
        }
    }
    var maskChar:Character
    init(frame: CGRect = CGRectZero, maskChar:Character = “*”) {
        print(“frame=\(frame), maskChar=\(maskChar)”)
        self.realText = “”
        self.maskChar = maskChar
        super.init(frame: frame)
        self.secureTextEntry = false
        self.addTarget(self, action: #selector(self.textFiledEditingChanged(_:)), forControlEvents: UIControlEvents.EditingChanged)
        self.delegate = self
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError(“init(coder:) has not been implemented”)
    }
    func updateMaskStr(){
        print(“before update: self.text=\(self.text), self.realText=\(self.realText)”)
        //change real text to mask char
        var maskStr = “”
        for _ in self.realText.characters {
            maskStr += String(self.maskChar)
        }
        self.text = maskStr
        print(“after  update: self.text=\(self.text), self.realText=\(self.realText)”)
    }
    func textFiledEditingChanged(textField: UITextField) {
        print(“textField=\(textField), textField.text=\(textField.text)”)
        updateMaskStr()
    }
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        print(“textField=\(textField), range=\(range), string=\(string)”)
        var allow = true
        let curText = self.realText
        let updatedStr:String = (curText as NSString).stringByReplacingCharactersInRange(range, withString: string)
        if updatedStr.characters.count > LoginRegisterPasswordLengthMax {
            print(“password exceed max length \(LoginRegisterPasswordLengthMax)”)
            allow = false
        }
        if allow {
            self.realText = updatedStr
        }
        print(“curText=\(curText), updatedStr=\(updatedStr), self.realText=\(self.realText)”)
        return false
    }
}

调用方式:

EditInfoView.swift:
class EditInfoView: UIView {
    var passwordTextField:PasswordTextField
    init(editMode:EditInfoMode) {
        self.passwordTextField = PasswordTextField()
        self.addSubview(self.passwordTextField)
        if self.passwordTextField.notHidden {
            //5. password text
            var passwordPlaceholder = “密码(6-20位)”
            if self.editMode == .ChangeLoginPassword {
                passwordPlaceholder = “旧密码(6-20位)”
            } else if self.editMode == .ForgotPassword {
                passwordPlaceholder = “输入新密码(6-20位)”
            }
            self.passwordTextField.placeholder = passwordPlaceholder
            self.passwordTextField.returnKeyType = UIReturnKeyType.Go
//            self.passwordTextField.secureTextEntry = true
            constrain(passwordTextField, smsCodeTextField, phoneTextField) {passwordTextField, smsCodeTextField, phoneTextField in
                passwordTextField.centerX == passwordTextField.superview!.centerX
                passwordTextField.width == passwordTextField.superview!.width – 2 * LoginRegisterPaddingX
                passwordTextField.height == CommonTextFieldHeight
                if self.editMode == .ChangeLoginPassword {
                    passwordTextField.top == passwordTextField.superview!.top + EditInfoViewTopPadding
                } else {
                    passwordTextField.top == smsCodeTextField.bottom + EditInfoViewCommonPaddingY
                }
            }
        }
}
EditInfoViewController.swift:
isValid = validatePassword(self, alertPrefix: alertPrefix, passwordStr: self.editInfoView.passwordTextField.realText, doAlertWhenInvalid: doAlertWhenInvalid)

可以工作了:

转载请注明:在路上 » 【已解决】swift中把secureTextEntry的textfield的密码输入框输入的内容从原点换成星号

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.307 seconds, using 22.06MB memory