Color passwords (red digits, blue punctuations)

This commit is contained in:
yishilin14 2017-02-24 00:24:39 +08:00
parent 5f872e0345
commit 3e0ba8a1d5

View file

@ -33,6 +33,7 @@ class LabelTableViewCell: UITableViewCell {
} else {
contentLabel.text = cellData?.content
}
contentLabel.font = UIFont(name: "Menlo", size: contentLabel.font.pointSize)
}
}
@ -69,7 +70,22 @@ class LabelTableViewCell: UITableViewCell {
}
func revealPassword(_ sender: Any?) {
contentLabel.text = cellData?.content
if let plainPassword = cellData?.content {
let attributePassword = NSMutableAttributedString.init(string: plainPassword)
// draw all digits in the password into red
// draw all punctuation characters in the password into blue
for (index, element) in plainPassword.unicodeScalars.enumerated() {
if NSCharacterSet.decimalDigits.contains(element) {
attributePassword.addAttribute(NSForegroundColorAttributeName, value: UIColor.red , range: NSRange(location: index, length: 1))
} else if NSCharacterSet.punctuationCharacters.contains(element) {
attributePassword.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue , range: NSRange(location: index, length: 1))
}
}
// set contentLabel
contentLabel.attributedText = attributePassword
} else {
contentLabel.text = ""
}
isReveal = true
}