265 lines
11 KiB
Swift
265 lines
11 KiB
Swift
//
|
|
// LabelViewController.swift
|
|
// VDSSample
|
|
//
|
|
// Created by Matt Bruce on 8/16/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import VDS
|
|
import VDSColorTokens
|
|
|
|
class LabelViewController: BaseViewController<Label> {
|
|
|
|
lazy var textSizePickerSelectorView = {
|
|
TextSizePickerSelectorView(title: "",
|
|
picker: self.picker)
|
|
}()
|
|
|
|
lazy var textPositionPickerSelectorView = {
|
|
PickerSelectorView(title: TextPosition.left.rawValue,
|
|
picker: self.picker,
|
|
items: TextPosition.allCases)
|
|
}()
|
|
|
|
lazy var fontCategoryPickerSelectorView = {
|
|
FontCategoryPickerSelectorView(title: "",
|
|
picker: self.picker)
|
|
}()
|
|
//------------------------------------
|
|
//testing only remove later
|
|
//------------------------------------
|
|
var pointSizeRange = Slider()
|
|
var lineheightRange = Slider()
|
|
var lineSpacingRange = Slider()
|
|
var topInsetRange = Slider()
|
|
var bottomInsetRange = Slider()
|
|
//------------------------------------
|
|
|
|
var currentStyle: TextStyle!
|
|
var boldSwitch = Toggle()
|
|
var disabledSwitch = Toggle()
|
|
var textField = TextField()
|
|
private var isBold: Bool = false
|
|
var lineHeightLabel = UILabel().with { $0.translatesAutoresizingMaskIntoConstraints = false; $0.lineBreakMode = .byWordWrapping; $0.numberOfLines = 0}
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
let stack = UIStackView()
|
|
stack.axis = .vertical
|
|
stack.spacing = 10
|
|
stack.addArrangedSubview(component)
|
|
stack.addArrangedSubview(lineHeightLabel)
|
|
|
|
addContentTopView(view: component)
|
|
|
|
setupPicker()
|
|
setupModel()
|
|
}
|
|
var defaultLineHeightLabel: UILabel?
|
|
var defaultPointLabel: UILabel?
|
|
var calculatedLabel = UILabel()
|
|
override func allTextFields() -> [TextField]? { [textField] }
|
|
|
|
override func setupForm(){
|
|
super.setupForm()
|
|
addFormRow(label: "Disabled", view: disabledSwitch)
|
|
addFormRow(label: "Bold", view: boldSwitch)
|
|
addFormRow(label: "Surface", view: surfacePickerSelectorView)
|
|
addFormRow(label: "Text Position", view: textPositionPickerSelectorView)
|
|
addFormRow(label: "Font Category", view: fontCategoryPickerSelectorView)
|
|
addFormRow(label: "Text Size", view: textSizePickerSelectorView)
|
|
addFormRow(label: "Text", view: textField)
|
|
|
|
disabledSwitch.onChange = { [weak self] sender in
|
|
self?.component.disabled = sender.isOn
|
|
}
|
|
|
|
boldSwitch
|
|
.publisher(for: .valueChanged)
|
|
.sink { [weak self] sender in
|
|
self?.isBold = sender.isOn
|
|
self?.updateLabelStyle()
|
|
}.store(in: &subscribers)
|
|
|
|
textField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.text = text
|
|
self?.lineHeightLabel.text = text
|
|
}.store(in: &subscribers)
|
|
|
|
//--------------------------------------------------------------
|
|
#warning("testing only remove later")
|
|
//--------------------------------------------------------------
|
|
let testingSection = FormSection()
|
|
testingSection.title = "Temporary Settings"
|
|
testingSection.addFormRow(label: "Calculated", view: calculatedLabel)
|
|
let pointView = testingSection.addFormRow(label: "Font Size", view: pointSizeRange)
|
|
let view = testingSection.addFormRow(label: "Line Height", view: lineheightRange)
|
|
testingSection.addFormRow(label: "Top Inset", view: topInsetRange)
|
|
testingSection.addFormRow(label: "Bottom Inset", view: bottomInsetRange)
|
|
defaultLineHeightLabel = view.viewWithTag(1) as? UILabel
|
|
defaultPointLabel = pointView.viewWithTag(1) as? UILabel
|
|
append(section: testingSection)
|
|
|
|
pointSizeRange.maximumValue = 20.0
|
|
pointSizeRange.minimumValue = 0.0
|
|
pointSizeRange.value = 0.0
|
|
pointSizeRange.publisher(for: .valueChanged).sink(receiveValue: { [weak self] slider in
|
|
guard let self else { return }
|
|
let textStyle = self.component.textStyle
|
|
self.component.textStyle = TextStyle(rawValue: textStyle.rawValue,
|
|
fontFace: textStyle.fontFace,
|
|
pointSize: CGFloat(slider.value),
|
|
lineHeight: textStyle.lineHeight,
|
|
letterSpacing: textStyle.letterSpacing,
|
|
edgeInsets: textStyle.edgeInsets)
|
|
}).store(in: &subscribers)
|
|
|
|
lineheightRange.maximumValue = 20.0
|
|
lineheightRange.minimumValue = 0.0
|
|
lineheightRange.value = 2.0
|
|
lineheightRange.publisher(for: .valueChanged).sink(receiveValue: { [weak self] slider in
|
|
guard let self else { return }
|
|
let textStyle = self.component.textStyle
|
|
self.component.textStyle = TextStyle(rawValue: textStyle.rawValue,
|
|
fontFace: textStyle.fontFace,
|
|
pointSize: textStyle.pointSize,
|
|
lineHeight: CGFloat(slider.value),
|
|
letterSpacing: textStyle.letterSpacing,
|
|
edgeInsets: textStyle.edgeInsets)
|
|
}).store(in: &subscribers)
|
|
|
|
topInsetRange.maximumValue = 20.0
|
|
topInsetRange.minimumValue = -20
|
|
topInsetRange.value = 0
|
|
topInsetRange.publisher(for: .valueChanged).sink(receiveValue: { [weak self] slider in
|
|
guard let self else { return }
|
|
let textStyle = self.component.textStyle
|
|
self.component.textStyle = TextStyle(rawValue: textStyle.rawValue,
|
|
fontFace: textStyle.fontFace,
|
|
pointSize: textStyle.pointSize,
|
|
lineHeight: textStyle.lineHeight,
|
|
letterSpacing: textStyle.letterSpacing,
|
|
edgeInsets: .init(top: CGFloat(slider.value), left: 0, bottom: CGFloat(bottomInsetRange.value), right: 0))
|
|
}).store(in: &subscribers)
|
|
|
|
bottomInsetRange.maximumValue = 20.0
|
|
bottomInsetRange.minimumValue = -20
|
|
bottomInsetRange.value = 0
|
|
bottomInsetRange.publisher(for: .valueChanged).sink(receiveValue: { [weak self] slider in
|
|
guard let self else { return }
|
|
let textStyle = self.component.textStyle
|
|
self.component.textStyle = TextStyle(rawValue: textStyle.rawValue,
|
|
fontFace: textStyle.fontFace,
|
|
pointSize: textStyle.pointSize,
|
|
lineHeight: textStyle.lineHeight,
|
|
letterSpacing: textStyle.letterSpacing,
|
|
edgeInsets: .init(top: CGFloat(topInsetRange.value), left: 0, bottom: CGFloat(slider.value), right: 0))
|
|
}).store(in: &subscribers)
|
|
|
|
component.publisher(for: \.bounds)
|
|
.sink { [weak self] rect in
|
|
self?.calculatedLabel.text = "Actual height: \(rect.height)"
|
|
}.store(in: &subscribers)
|
|
|
|
//------------------------------------
|
|
}
|
|
|
|
func setupModel() {
|
|
component.text = "Helg¹ worldg Hell¹ worldg"
|
|
component.textStyle = .featureXLarge
|
|
lineHeightLabel.text = component.text
|
|
lineHeightLabel.font = component.textStyle.font
|
|
|
|
//setup UI
|
|
surfacePickerSelectorView.text = component.surface.rawValue
|
|
disabledSwitch.isOn = component.disabled
|
|
boldSwitch.isOn = isBold
|
|
textField.text = component.text
|
|
|
|
//set the font
|
|
fontCategory = .feature
|
|
fontCategoryPickerSelectorView.text = "Feature"
|
|
textSize = .xlarge
|
|
textSizePickerSelectorView.text = "XLarge"
|
|
updateLabelStyle()
|
|
|
|
}
|
|
|
|
override func showDebug(show: Bool) {
|
|
component.layer.borderColor = UIColor.blue.cgColor
|
|
component.layer.borderWidth = show ? 1 : 0
|
|
|
|
lineHeightLabel.layer.borderColor = UIColor.blue.cgColor
|
|
lineHeightLabel.layer.borderWidth = show ? 1 : 0
|
|
}
|
|
|
|
//Picker
|
|
private var fontCategory: TextStyle.FontCategory = .feature {
|
|
didSet {
|
|
fontCategoryPickerSelectorView.text = fontCategory.rawValue
|
|
textSizePickerSelectorView.items = fontCategory.sizes
|
|
if textSizePickerSelectorView.items.count > 0 {
|
|
textSize = textSizePickerSelectorView.items[0]
|
|
} else {
|
|
textSize = nil
|
|
}
|
|
updateLabelStyle()
|
|
}
|
|
}
|
|
|
|
private var textSize: TextStyle.FontSize? = .large {
|
|
didSet {
|
|
textSizePickerSelectorView.text = textSize?.rawValue ?? ""
|
|
updateLabelStyle()
|
|
}
|
|
}
|
|
|
|
func updateLabelStyle(){
|
|
if let style = fontCategory.style(for: textSize, isBold: isBold) {
|
|
component.textStyle = style
|
|
currentStyle = style
|
|
|
|
//--------------------------------------------------------------
|
|
#warning("testing only remove later")
|
|
//--------------------------------------------------------------
|
|
let defaultLineHeight = style.font.lineHeight
|
|
lineHeightLabel.font = style.font
|
|
lineheightRange.maximumValue = Float(defaultLineHeight) * 1.50
|
|
lineheightRange.minimumValue = Float(defaultLineHeight) * 0.50
|
|
lineheightRange.value = Float(style.lineHeight)
|
|
pointSizeRange.value = Float(style.pointSize)
|
|
pointSizeRange.maximumValue = Float(style.pointSize) * 1.50
|
|
pointSizeRange.minimumValue = Float(style.pointSize) * 0.50
|
|
topInsetRange.value = Float(style.edgeInsets.top)
|
|
bottomInsetRange.value = Float(style.edgeInsets.bottom)
|
|
defaultLineHeightLabel?.text = "Line Height - d:\(defaultLineHeight) s:\(style.lineHeight)"
|
|
defaultPointLabel?.text = "Font Size - s:\(style.pointSize)"
|
|
//--------------------------------------------------------------
|
|
}
|
|
}
|
|
|
|
func setupPicker(){
|
|
surfacePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.surface = item
|
|
self?.contentTopView.backgroundColor = item.color
|
|
}
|
|
|
|
textPositionPickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.component.textPosition = item
|
|
}
|
|
|
|
textSizePickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.textSize = item
|
|
}
|
|
|
|
fontCategoryPickerSelectorView.onPickerDidSelect = { [weak self] item in
|
|
self?.fontCategory = item
|
|
}
|
|
}
|
|
|
|
}
|
|
|