178 lines
5.3 KiB
Swift
178 lines
5.3 KiB
Swift
//
|
|
// LabelViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 8/16/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSColorTokens
|
|
|
|
class LabelViewController: ModelViewController<DefaultLabelModel>, StoryboardInitable {
|
|
deinit {
|
|
print("\(Self.self) deinit")
|
|
}
|
|
|
|
enum PickerType {
|
|
case surface, textSize, fontCategory
|
|
}
|
|
|
|
static var storyboardId: String = "label"
|
|
static var storyboardName: String = "Components"
|
|
|
|
@IBOutlet weak var containerView: UIView!
|
|
@IBOutlet weak var picker: UIPickerView!
|
|
@IBOutlet weak var surfaceLabel: UILabel!
|
|
@IBOutlet weak var textSizeLabel: UILabel!
|
|
@IBOutlet weak var fontCategoryLabel: UILabel!
|
|
@IBOutlet weak var boldswitch: UISwitch!
|
|
@IBOutlet weak var disabledSwitch: UISwitch!
|
|
@IBOutlet weak var textField: UITextField!
|
|
private var isBold: Bool = false
|
|
|
|
var label: Label!
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
label = Label()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
containerView.addSubview(label)
|
|
label.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 20).isActive = true
|
|
label.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 20).isActive = true
|
|
label.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -20).isActive = true
|
|
label.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -20).isActive = true
|
|
|
|
view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))
|
|
setupPicker()
|
|
setupModel()
|
|
}
|
|
|
|
func setupModel() {
|
|
var defaultModel = DefaultLabelModel()
|
|
defaultModel.text = "Label Component"
|
|
defaultModel.typograpicalStyle = .FeatureSmall
|
|
set(with: defaultModel)
|
|
|
|
label
|
|
.handlerPublisher()
|
|
.sink { [weak self] viewModel in
|
|
self?.model = viewModel
|
|
}.store(in: &subscribers)
|
|
|
|
//setup UI
|
|
surfaceLabel.text = model.surface.rawValue
|
|
disabledSwitch.isOn = model.disabled
|
|
boldswitch.isOn = isBold
|
|
textField.text = model.text
|
|
|
|
//set the font
|
|
fontCategory = .feature
|
|
fontCategoryLabel.text = "Feature"
|
|
textSize = .small
|
|
textSizeLabel.text = "Small"
|
|
}
|
|
|
|
override func updateView(viewModel: DefaultLabelModel) {
|
|
label.set(with: viewModel)
|
|
}
|
|
|
|
@IBAction func disabledChanged(_ sender: UISwitch) {
|
|
label.isEnabled = !sender.isOn
|
|
}
|
|
|
|
@IBAction func boldChanged(_ sender: UISwitch) {
|
|
isBold = sender.isOn
|
|
updateLabelStyle()
|
|
}
|
|
|
|
@IBAction func textDidEnd(_ sender: UITextField) {
|
|
label.text = sender.text?.replacingOccurrences(of: "\\n", with: "\n") ?? "Label Component"
|
|
sender.resignFirstResponder()
|
|
}
|
|
|
|
@IBAction func surfaceClick(_ sender: Any) {
|
|
pickerType = .surface
|
|
}
|
|
|
|
@IBAction func textSizeClick(_ sender: Any) {
|
|
pickerType = .textSize
|
|
}
|
|
|
|
@IBAction func fontCategoryClick(_ sender: Any) {
|
|
pickerType = .fontCategory
|
|
}
|
|
|
|
//Picker
|
|
var surfacePicker = SurfacePicker()
|
|
var textSizePicker = TextSizePicker()
|
|
var fontCategoryPicker = FontCategoryPicker()
|
|
|
|
var pickerType: PickerType = .surface {
|
|
didSet {
|
|
func update(object: UIPickerViewDelegate & UIPickerViewDataSource){
|
|
picker.delegate = object
|
|
picker.dataSource = object
|
|
}
|
|
|
|
switch pickerType{
|
|
case .surface:
|
|
update(object: surfacePicker)
|
|
case .textSize:
|
|
update(object: textSizePicker)
|
|
case .fontCategory:
|
|
update(object: fontCategoryPicker)
|
|
}
|
|
picker.reloadAllComponents()
|
|
picker.selectRow(0, inComponent: 0, animated: false)
|
|
picker.isHidden = false
|
|
}
|
|
}
|
|
|
|
private var fontCategory: TypographicalStyle.FontCategory = .feature {
|
|
didSet {
|
|
fontCategoryLabel.text = fontCategory.rawValue
|
|
textSizePicker.items = fontCategory.sizes
|
|
if textSizePicker.items.count > 0 {
|
|
textSize = textSizePicker.items[0]
|
|
} else {
|
|
textSize = nil
|
|
}
|
|
updateLabelStyle()
|
|
}
|
|
}
|
|
|
|
private var textSize: TypographicalStyle.FontSize? = .large {
|
|
didSet {
|
|
textSizeLabel.text = textSize?.rawValue ?? ""
|
|
updateLabelStyle()
|
|
}
|
|
|
|
}
|
|
|
|
func updateLabelStyle(){
|
|
if let style = fontCategory.style(for: textSize, isBold: isBold) {
|
|
label.model.typograpicalStyle = style
|
|
}
|
|
}
|
|
|
|
func setupPicker(){
|
|
picker.isHidden = true
|
|
surfacePicker.onPickerDidSelect = { [weak self] item in
|
|
self?.label.surface = item
|
|
self?.containerView.backgroundColor = item.color
|
|
self?.surfaceLabel.text = item.rawValue
|
|
}
|
|
|
|
textSizePicker.onPickerDidSelect = { [weak self] item in
|
|
self?.textSize = item
|
|
}
|
|
|
|
fontCategoryPicker.onPickerDidSelect = { [weak self] item in
|
|
self?.fontCategory = item
|
|
}
|
|
}
|
|
}
|