fixed issue with keyboard in label not having a minus sign added helpers in Label for looking at scaled mode added min lineheight Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
309 lines
12 KiB
Swift
309 lines
12 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 useMinimumLineHeight = Toggle()
|
|
var pointSizeRange = Slider().with {
|
|
$0.minimumValue = 5
|
|
$0.value = 0
|
|
}
|
|
var lineheightRange = Slider()
|
|
var lineSpacingRange = Slider()
|
|
var topInsetRange = Slider().with {
|
|
$0.maximumValue = 100
|
|
$0.minimumValue = -100
|
|
$0.value = 0
|
|
}
|
|
var bottomInsetRange = Slider().with {
|
|
$0.maximumValue = 100
|
|
$0.minimumValue = -100
|
|
$0.value = 0
|
|
}
|
|
//------------------------------------
|
|
var scaledMode: Bool { component.useScaledFont && TextStyle.canScaleFonts }
|
|
var baseStyle: TextStyle!
|
|
var currentStyle: TextStyle! {
|
|
didSet {
|
|
//--------------------------------------------------------------
|
|
#warning("testing only remove later")
|
|
//--------------------------------------------------------------
|
|
let lineheight = scaledMode ? currentStyle.scaledLineHeight : currentStyle.lineHeight
|
|
let lineheightText = scaledMode ? "\(baseStyle.lineHeight.rounded()) sc\(currentStyle.scaledLineHeight)" : "\(baseStyle.lineHeight.rounded())"
|
|
|
|
pointSizeRange.value = Float(currentStyle.pointSize)
|
|
pointSizeRange.maximumValue = Float(baseStyle.pointSize * 1.50)
|
|
lineheightRange.value = Float(lineheight)
|
|
lineheightRange.maximumValue = Float((scaledMode ? baseStyle.scaledLineHeight : lineheight) * 1.50)
|
|
lineheightRange.minimumValue = 1
|
|
|
|
topInsetRange.value = Float(currentStyle.edgeInsets.top)
|
|
bottomInsetRange.value = Float(currentStyle.edgeInsets.bottom)
|
|
defaultLineHeightLabel?.text = "Line Height - iOS:\(Int(currentFont.lineHeight.rounded())) VDS:\(lineheightText)"
|
|
let text = scaledMode ? "iOS: \(Int(currentFont.pointSize.rounded()))" : ""
|
|
defaultPointLabel?.text = "Font Size - \(text) VDS:\(currentStyle.pointSize.rounded())"
|
|
//--------------------------------------------------------------
|
|
}
|
|
}
|
|
var useScaledFont = Toggle()
|
|
var boldSwitch = Toggle()
|
|
var disabledSwitch = Toggle()
|
|
var textField = TextField()
|
|
private var isBold: Bool = false
|
|
|
|
override func setup() {
|
|
super.setup()
|
|
|
|
NotificationCenter
|
|
.Publisher(center: .default, name: UIContentSizeCategory.didChangeNotification)
|
|
.sink { [weak self] notification in
|
|
self?.checkForScaledFonts()
|
|
}.store(in: &subscribers)
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
addContentTopView(view: component)
|
|
setupPicker()
|
|
setupModel()
|
|
}
|
|
var defaultLineHeightLabel: Label?
|
|
var defaultPointLabel: Label?
|
|
var calculatedLabel = UILabel()
|
|
var scaledFontRow: UIView?
|
|
|
|
func checkForScaledFonts() {
|
|
scaledFontRow?.isHidden = !TextStyle.canScaleFonts
|
|
updateLabelStyle()
|
|
}
|
|
|
|
override func setupForm(){
|
|
super.setupForm()
|
|
addFormRow(label: "Disabled", view: disabledSwitch)
|
|
addFormRow(label: "Bold", view: boldSwitch)
|
|
addFormRow(label: "Surface", view: surfacePickerSelectorView)
|
|
|
|
//Scaled Fonts implementation
|
|
scaledFontRow = addFormRow(label: "Use Scaled Font", view: useScaledFont)
|
|
checkForScaledFonts()
|
|
|
|
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)
|
|
|
|
useScaledFont
|
|
.publisher(for: .valueChanged)
|
|
.sink { [weak self] sender in
|
|
self?.component.useScaledFont = sender.isOn
|
|
self?.updateLabelStyle()
|
|
}.store(in: &subscribers)
|
|
|
|
textField
|
|
.textPublisher
|
|
.sink { [weak self] text in
|
|
self?.component.text = text
|
|
}.store(in: &subscribers)
|
|
|
|
//--------------------------------------------------------------
|
|
#warning("testing only remove later")
|
|
//--------------------------------------------------------------
|
|
let testingSection = FormSection()
|
|
testingSection.title = "Temporary Settings"
|
|
testingSection.addFormRow(label: "Calculated", view: calculatedLabel)
|
|
testingSection.addFormRow(label: "Min LineHeight", view: useMinimumLineHeight)
|
|
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? Label
|
|
defaultPointLabel = pointView.viewWithTag(1) as? Label
|
|
append(section: testingSection)
|
|
|
|
useMinimumLineHeight
|
|
.publisher(for: .valueChanged)
|
|
.sink { [weak self] toggle in
|
|
NSMutableAttributedString.useMinimumLineHeight = toggle.isOn
|
|
self?.component.setNeedsUpdate()
|
|
}.store(in: &subscribers)
|
|
|
|
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: slider.rounded,
|
|
lineHeight: textStyle.lineHeight,
|
|
letterSpacing: textStyle.letterSpacing,
|
|
edgeInsets: textStyle.edgeInsets)
|
|
self.currentStyle = self.component.textStyle
|
|
}).store(in: &subscribers)
|
|
|
|
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: slider.rounded,
|
|
letterSpacing: textStyle.letterSpacing,
|
|
edgeInsets: textStyle.edgeInsets)
|
|
self.currentStyle = self.component.textStyle
|
|
}).store(in: &subscribers)
|
|
|
|
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: slider.rounded, left: 0, bottom: CGFloat(bottomInsetRange.value), right: 0))
|
|
self.currentStyle = self.component.textStyle
|
|
}).store(in: &subscribers)
|
|
|
|
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: slider.rounded, right: 0))
|
|
self.currentStyle = self.component.textStyle
|
|
}).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"
|
|
|
|
//setup UI
|
|
surfacePickerSelectorView.text = component.surface.rawValue
|
|
disabledSwitch.isOn = component.disabled
|
|
boldSwitch.isOn = isBold
|
|
textField.text = component.text
|
|
|
|
//set the font
|
|
component.textStyle = .featureMedium
|
|
fontCategory = .feature
|
|
fontCategoryPickerSelectorView.text = fontCategory.rawValue.capitalized
|
|
textSize = .medium
|
|
textSizePickerSelectorView.text = textSize!.rawValue.capitalized
|
|
updateLabelStyle()
|
|
|
|
}
|
|
|
|
override func showDebug(show: Bool) {
|
|
component.layer.borderColor = UIColor.blue.cgColor
|
|
component.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? = .medium {
|
|
didSet {
|
|
textSizePickerSelectorView.text = textSize?.rawValue ?? ""
|
|
updateLabelStyle()
|
|
}
|
|
}
|
|
|
|
var currentFont: UIFont {
|
|
|
|
scaledMode ? currentStyle.font.scaledFont : currentStyle.font
|
|
}
|
|
|
|
func updateLabelStyle(){
|
|
if let style = fontCategory.style(for: textSize, isBold: isBold) {
|
|
component.textStyle = style
|
|
baseStyle = style
|
|
currentStyle = style
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension Slider {
|
|
var rounded: CGFloat {
|
|
CGFloat((value * 100).rounded() / 100)
|
|
}
|
|
}
|
|
|