passforios/pass/Views/LabelTableViewCell.swift

102 lines
3.3 KiB
Swift
Raw Normal View History

2017-02-02 21:04:31 +08:00
//
// LabelTableViewCell.swift
// pass
//
// Created by Mingshen Sun on 2/2/2017.
// Copyright © 2017 Bob Sun. All rights reserved.
//
import UIKit
2017-02-05 13:56:37 +08:00
struct LabelTableViewCellData {
var title: String
var content: String
}
2017-02-02 21:04:31 +08:00
class LabelTableViewCell: UITableViewCell {
@IBOutlet weak var contentLabel: UILabel!
@IBOutlet weak var titleLabel: UILabel!
2017-02-05 13:56:37 +08:00
var isPasswordCell = false
2017-02-08 17:55:18 +08:00
var isURLCell = false
var isReveal = false
2017-02-08 17:55:18 +08:00
var password: Password?
let passwordDots = "••••••••••••"
2017-02-05 13:56:37 +08:00
var cellData: LabelTableViewCellData? {
didSet {
titleLabel.text = cellData?.title
if isPasswordCell {
contentLabel.text = passwordDots
2017-02-24 21:33:36 +08:00
contentLabel.font = UIFont(name: "Menlo", size: contentLabel.font.pointSize)
} else {
contentLabel.text = cellData?.content
}
2017-02-05 13:56:37 +08:00
}
}
2017-02-02 21:04:31 +08:00
2017-02-05 00:35:23 +08:00
override var canBecomeFirstResponder: Bool {
get {
return true
}
}
2017-02-02 21:04:31 +08:00
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
2017-02-05 00:35:23 +08:00
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(_:))
}
}
2017-02-08 17:55:18 +08:00
if isURLCell {
return action == #selector(copy(_:)) || action == #selector(LabelTableViewCell.openLink(_:))
}
2017-02-05 00:35:23 +08:00
return action == #selector(copy(_:))
}
2017-02-05 00:35:23 +08:00
override func copy(_ sender: Any?) {
2017-02-23 17:56:12 +08:00
Utils.copyToPasteboard(textToCopy: cellData?.content)
2017-02-05 00:35:23 +08:00
}
2017-02-08 17:55:18 +08:00
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: 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
}
func concealPassword(_ sender: Any?) {
contentLabel.text = passwordDots
isReveal = false
}
2017-02-08 17:55:18 +08:00
func openLink(_ sender: Any?) {
2017-02-23 17:56:12 +08:00
Utils.copyToPasteboard(textToCopy: password?.password)
2017-02-15 15:29:06 +08:00
UIApplication.shared.open(URL(string: cellData!.content)!, options: [:], completionHandler: nil)
2017-02-08 17:55:18 +08:00
}
2017-02-02 21:04:31 +08:00
}