diff --git a/pass/Helpers/Utils.swift b/pass/Helpers/Utils.swift index 4fcbdad..bef1307 100644 --- a/pass/Helpers/Utils.swift +++ b/pass/Helpers/Utils.swift @@ -125,6 +125,19 @@ class Utils { } } } + static func attributedPassword(plainPassword: String) -> NSAttributedString{ + let attributedPassword = 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) { + attributedPassword.addAttribute(NSForegroundColorAttributeName, value: Globals.red, range: NSRange(location: index, length: 1)) + } else if NSCharacterSet.punctuationCharacters.contains(element) { + attributedPassword.addAttribute(NSForegroundColorAttributeName, value: Globals.blue, range: NSRange(location: index, length: 1)) + } + } + return attributedPassword + } } // https://gist.github.com/NikolaiRuhe/eeb135d20c84a7097516 diff --git a/pass/Views/FillPasswordTableViewCell.swift b/pass/Views/FillPasswordTableViewCell.swift index fdf8bd4..8ea7d97 100644 --- a/pass/Views/FillPasswordTableViewCell.swift +++ b/pass/Views/FillPasswordTableViewCell.swift @@ -23,14 +23,15 @@ class FillPasswordTableViewCell: ContentTableViewCell { } @IBAction func generatePassword(_ sender: UIButton) { - contentTextField.text = Utils.randomString(length: 16) + let plainPassword = Utils.randomString(length: 16) + contentTextField.attributedText = Utils.attributedPassword(plainPassword: plainPassword) } override func getContent() -> String? { - return contentTextField.text + return contentTextField.attributedText?.string } override func setContent(content: String) { - contentTextField.text = content + contentTextField.attributedText = Utils.attributedPassword(plainPassword: content) } } diff --git a/pass/Views/LabelTableViewCell.swift b/pass/Views/LabelTableViewCell.swift index fcf3833..0bf0601 100644 --- a/pass/Views/LabelTableViewCell.swift +++ b/pass/Views/LabelTableViewCell.swift @@ -71,18 +71,7 @@ class LabelTableViewCell: UITableViewCell { func revealPassword(_ sender: Any?) { 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: Globals.red, range: NSRange(location: index, length: 1)) - } else if NSCharacterSet.punctuationCharacters.contains(element) { - attributePassword.addAttribute(NSForegroundColorAttributeName, value: Globals.blue, range: NSRange(location: index, length: 1)) - } - } - // set contentLabel - contentLabel.attributedText = attributePassword + contentLabel.attributedText = Utils.attributedPassword(plainPassword: plainPassword) } else { contentLabel.text = "" }