passepartout-apple/Passepartout/App/iOS/Cells/FieldTableViewCell.swift

138 lines
3.7 KiB
Swift
Raw Normal View History

2018-10-11 07:13:19 +00:00
//
// FieldTableViewCell.swift
2021-01-01 16:53:28 +00:00
// Passepartout
2018-10-11 07:13:19 +00:00
//
// Created by Davide De Rosa on 6/13/18.
2020-12-27 16:30:25 +00:00
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
2018-10-11 07:13:19 +00:00
//
2018-11-03 21:33:30 +00:00
// https://github.com/passepartoutvpn
2018-10-11 07:13:19 +00:00
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
extension Cells {
static let field = FieldTableViewCell.Provider()
}
2021-06-26 07:49:48 +00:00
protocol FieldTableViewCellDelegate: AnyObject {
2018-10-11 07:13:19 +00:00
func fieldCellDidEdit(_: FieldTableViewCell)
func fieldCellDidEnter(_: FieldTableViewCell)
}
class FieldTableViewCell: UITableViewCell {
var caption: String? {
get {
return textLabel?.text
}
set {
textLabel?.text = newValue
}
}
var captionColor: UIColor? {
get {
return textLabel?.textColor
}
set {
textLabel?.textColor = newValue
}
}
var captionWidth: CGFloat = 0.0 {
didSet {
layoutSubviews()
}
}
var allowedCharset: CharacterSet? {
didSet {
illegalCharset = allowedCharset?.inverted
}
}
private var illegalCharset: CharacterSet?
2018-10-11 07:13:19 +00:00
private(set) lazy var field = UITextField()
weak var delegate: FieldTableViewCellDelegate?
override func awakeFromNib() {
super.awakeFromNib()
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.textAlignment = .left
field.delegate = self
selectionStyle = .none
contentView.addSubview(field)
}
override func layoutSubviews() {
super.layoutSubviews()
var frame: CGRect
let label: UILabel = textLabel!
frame = label.frame
frame.size.width = captionWidth
label.frame = frame
let offset: CGFloat = 15.0
field.frame = CGRect(
x: label.frame.maxX,
y: 0.0,
width: bounds.size.width - label.frame.maxX - offset,
height: bounds.size.height
)
}
}
extension FieldTableViewCell: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let illegalCharset = illegalCharset else {
return true
}
guard string.rangeOfCharacter(from: illegalCharset) == nil else {
return false
}
return true
}
2018-10-11 07:13:19 +00:00
func textFieldDidEndEditing(_ textField: UITextField) {
delegate?.fieldCellDidEdit(self)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
delegate?.fieldCellDidEnter(self)
return true
}
}
extension FieldTableViewCell {
class Provider: CellProvider {
typealias T = FieldTableViewCell
func dequeue(from tableView: UITableView, for indexPath: IndexPath) -> FieldTableViewCell {
let cell = tableView.dequeue(T.self, identifier: Provider.identifier, for: indexPath)
cell.apply(.current)
2018-10-11 07:13:19 +00:00
return cell
}
}
}