parent
3ad1166d59
commit
7873631ff8
@ -0,0 +1,116 @@ |
||||
// Copyright © 2018 Stormbird PTE. LTD. |
||||
|
||||
import UIKit |
||||
|
||||
protocol AddressTextFieldDelegate: class { |
||||
func displayError(error: Error, for: AddressTextField) |
||||
func openQRCodeReader(for: AddressTextField) |
||||
func didPaste(in: AddressTextField) |
||||
func shouldReturn(in: AddressTextField) -> Bool |
||||
} |
||||
|
||||
class AddressTextField: UIControl { |
||||
let label = UILabel() |
||||
let textField = UITextField() |
||||
var value: String { |
||||
get { |
||||
return textField.text ?? "" |
||||
} |
||||
set { |
||||
textField.text = newValue |
||||
} |
||||
} |
||||
var isConfigured = false |
||||
weak var delegate: AddressTextFieldDelegate? |
||||
|
||||
override init(frame: CGRect) { |
||||
super.init(frame: frame) |
||||
|
||||
textField.translatesAutoresizingMaskIntoConstraints = false |
||||
textField.delegate = self |
||||
textField.leftViewMode = .always |
||||
textField.rightViewMode = .always |
||||
addSubview(textField) |
||||
|
||||
NSLayoutConstraint.activate([ |
||||
textField.leadingAnchor.constraint(equalTo: leadingAnchor), |
||||
textField.trailingAnchor.constraint(equalTo: trailingAnchor), |
||||
textField.topAnchor.constraint(equalTo: topAnchor), |
||||
textField.bottomAnchor.constraint(equalTo: bottomAnchor), |
||||
heightAnchor.constraint(equalToConstant: ScreenChecker().isNarrowScreen() ? 30 : 50), |
||||
]) |
||||
} |
||||
|
||||
required init?(coder aDecoder: NSCoder) { |
||||
fatalError("init(coder:) has not been implemented") |
||||
} |
||||
|
||||
override func layoutSubviews() { |
||||
super.layoutSubviews() |
||||
roundCornersBasedOnHeight() |
||||
} |
||||
|
||||
private func roundCornersBasedOnHeight() { |
||||
textField.layer.cornerRadius = textField.frame.size.height / 2 |
||||
} |
||||
|
||||
func configureOnce() { |
||||
guard !isConfigured else { return } |
||||
isConfigured = true |
||||
|
||||
label.font = Fonts.regular(size: 10)! |
||||
label.textColor = Colors.appGrayLabelColor |
||||
|
||||
textField.leftView = .spacerWidth(22) |
||||
textField.rightView = makeTargetAddressRightView() |
||||
textField.textColor = Colors.appText |
||||
textField.font = ScreenChecker().isNarrowScreen() ? Fonts.light(size: 11)! : Fonts.light(size: 15)! |
||||
textField.layer.borderColor = Colors.appBackground.cgColor |
||||
textField.layer.borderWidth = 1 |
||||
} |
||||
|
||||
private func makeTargetAddressRightView() -> UIView { |
||||
let pasteButton = Button(size: .normal, style: .borderless) |
||||
pasteButton.translatesAutoresizingMaskIntoConstraints = false |
||||
pasteButton.setTitle(R.string.localizable.sendPasteButtonTitle(), for: .normal) |
||||
pasteButton.titleLabel?.font = Fonts.regular(size: 14)! |
||||
pasteButton.setTitleColor(Colors.appGrayLabelColor, for: .normal) |
||||
pasteButton.addTarget(self, action: #selector(pasteAction), for: .touchUpInside) |
||||
|
||||
let scanQRCodeButton = Button(size: .normal, style: .borderless) |
||||
scanQRCodeButton.translatesAutoresizingMaskIntoConstraints = false |
||||
scanQRCodeButton.setImage(R.image.qr_code_icon(), for: .normal) |
||||
scanQRCodeButton.setTitleColor(Colors.appGrayLabelColor, for: .normal) |
||||
scanQRCodeButton.addTarget(self, action: #selector(openReader), for: .touchUpInside) |
||||
|
||||
let targetAddressRightView = [pasteButton, scanQRCodeButton].asStackView(distribution: .equalSpacing) |
||||
targetAddressRightView.translatesAutoresizingMaskIntoConstraints = false |
||||
|
||||
return targetAddressRightView |
||||
} |
||||
|
||||
@objc func pasteAction() { |
||||
guard let value = UIPasteboard.general.string?.trimmed else { |
||||
delegate?.displayError(error: SendInputErrors.emptyClipBoard, for: self) |
||||
return |
||||
} |
||||
|
||||
guard CryptoAddressValidator.isValidAddress(value) else { |
||||
delegate?.displayError(error: Errors.invalidAddress, for: self) |
||||
return |
||||
} |
||||
self.value = value |
||||
delegate?.didPaste(in: self) |
||||
} |
||||
|
||||
@objc func openReader() { |
||||
delegate?.openQRCodeReader(for: self) |
||||
} |
||||
} |
||||
|
||||
extension AddressTextField: UITextFieldDelegate { |
||||
func textFieldShouldReturn(_ textField: UITextField) -> Bool { |
||||
guard let delegate = delegate else { return true } |
||||
return delegate.shouldReturn(in: self) |
||||
} |
||||
} |
Loading…
Reference in new issue