show password as dots by default, and "reveal" and "conceal" menu

This commit is contained in:
Bob Sun 2017-02-06 11:55:27 +08:00
parent a7bd92c61f
commit ea6bc0a7bd
No known key found for this signature in database
GPG key ID: 1F86BA2052FED3B4
2 changed files with 29 additions and 1 deletions

View file

@ -19,11 +19,18 @@ class LabelTableViewCell: UITableViewCell {
@IBOutlet weak var contentLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
var isPasswordCell = false
var isReveal = false
let passwordDots = "••••••••••••"
var cellData: LabelTableViewCellData? {
didSet {
titleLabel.text = cellData?.title
contentLabel.text = cellData?.content
if isPasswordCell {
contentLabel.text = passwordDots
} else {
contentLabel.text = cellData?.content
}
}
}
@ -42,10 +49,27 @@ class LabelTableViewCell: UITableViewCell {
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if isPasswordCell {
if isReveal {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.concealPassword(_:))
} else {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.revealPassword(_:))
}
}
return action == #selector(copy(_:))
}
override func copy(_ sender: Any?) {
UIPasteboard.general.string = contentLabel.text
}
func revealPassword(_ sender: Any?) {
contentLabel.text = cellData?.content
isReveal = true
}
func concealPassword(_ sender: Any?) {
contentLabel.text = passwordDots
isReveal = false
}
}

View file

@ -59,6 +59,9 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
if let tappedCell = self.tableView.cellForRow(at: tapIndexPath) as? LabelTableViewCell {
tappedCell.becomeFirstResponder()
let menuController = UIMenuController.shared
let revealItem = UIMenuItem(title: "Reveal", action: #selector(LabelTableViewCell.revealPassword(_:)))
let concealItem = UIMenuItem(title: "Conceal", action: #selector(LabelTableViewCell.concealPassword(_:)))
menuController.menuItems = [revealItem, concealItem]
menuController.setTargetRect(tappedCell.contentLabel.frame, in: tappedCell.contentLabel.superview!)
menuController.setMenuVisible(true, animated: true)
}
@ -79,6 +82,7 @@ class PasswordDetailTableViewController: UITableViewController, UIGestureRecogni
let titleData = tableData[indexPath.section].item[indexPath.row].title
let contentData = tableData[indexPath.section].item[indexPath.row].content
cell.isPasswordCell = (titleData == "password" ? true : false)
cell.cellData = LabelTableViewCellData(title: titleData, content: contentData)
return cell
}