vds_ios_sample/VDSSample/ViewControllers/LabelViewController.swift
Matt Bruce d8a8caa957 converted to use isEnabled
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-08-25 15:07:54 -05:00

386 lines
15 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 useScaledLineHeight = Toggle().with { $0.isOn = NSMutableAttributedString.useScaledLineHeight }
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! {
didSet {
baseStyleLabel.text = descriptionFor(style: baseStyle,
useScaledLineHeight: useScaledLineHeight.isOn,
scaledMode: scaledMode)
}
}
var currentStyle: TextStyle! {
didSet {
//--------------------------------------------------------------
#warning("testing only remove later")
//--------------------------------------------------------------
pointSizeRange.value = Float(currentStyle.pointSize.rounded())
pointSizeRange.maximumValue = Float(baseStyle.pointSize * 1.50)
lineheightRange.value = Float(currentStyle.lineHeight.rounded())
lineheightRange.maximumValue = Float(pointSizeRange.value * 1.50)
lineheightRange.minimumValue = 1
topInsetRange.value = Float(currentStyle.edgeInsets.top).rounded()
bottomInsetRange.value = Float(currentStyle.edgeInsets.bottom).rounded()
updateStyleLabel()
//--------------------------------------------------------------
}
}
var useScaledFont = Toggle()
var boldSwitch = Toggle()
var disabledSwitch = Toggle()
var textField = TextField()
private var isBold: Bool = false
fileprivate func updateStyleLabel() {
baseStyleLabel.text = descriptionFor(style: baseStyle,
useScaledLineHeight: useScaledLineHeight.isOn,
scaledMode: scaledMode)
currentStyleLabel.text = descriptionFor(style: currentStyle,
useScaledLineHeight: useScaledLineHeight.isOn,
scaledMode: scaledMode)
}
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()
let fullText = "Here is a sample of text that has an inline text link that you can click on!"
let linkText = "inline text link"
let sampleLabel = Label()
sampleLabel.textStyle = .titleLarge
sampleLabel.text = fullText
if let link = ActionLabelAttribute(text: fullText, linkText: linkText) {
link.action.sink { [weak self] in
print("Clicked \(linkText)")
self?.present(UIAlertController(title: "TextLink", message: "Clicked \(linkText)", preferredStyle: .alert).with{ $0.addAction(.init(title: "OK", style: .default)) }, animated: true)
}.store(in: &subscribers)
sampleLabel.attributes = [link]
}
let stack = UIStackView(arrangedSubviews: [component, sampleLabel])
stack.spacing = 10
stack.axis = .vertical
addContentTopView(view: stack)
setupPicker()
setupModel()
}
var calculatedLabel = Label()
var baseStyleLabel = Label()
var scaledFontRow: UIView?
var scaledLineHeightRow: UIView?
var currentStyleRow: UIView?
var currentStyleLabel = Label()
func checkForScaledFonts() {
scaledFontRow?.isHidden = !TextStyle.canScaleFonts
scaledLineHeightRow?.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)
scaledLineHeightRow = addFormRow(label: "Use Scaled LineHeight", view: useScaledLineHeight)
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.isEnabled = !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: "Original Style Props", view: baseStyleLabel)
currentStyleRow = testingSection.addFormRow(label: "Customized Style Props", view: currentStyleLabel)
testingSection.addFormRow(label: "Calculated", view: calculatedLabel)
testingSection.addFormRow(label: "Point Size", view: pointSizeRange)
testingSection.addFormRow(label: "Line Height", view: lineheightRange)
testingSection.addFormRow(label: "Top Inset", view: topInsetRange)
testingSection.addFormRow(label: "Bottom Inset", view: bottomInsetRange)
append(section: testingSection)
useScaledLineHeight
.publisher(for: .valueChanged)
.sink { [weak self] toggle in
NSMutableAttributedString.useScaledLineHeight = toggle.isOn
self?.component.setNeedsUpdate()
self?.updateStyleLabel()
}.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 (* LineHeight x Text Lines): \(rect.height)"
}.store(in: &subscribers)
//------------------------------------
}
func setupModel() {
component.text = "Helg¹ worldg Hell¹ worldg"
//setup UI
surfacePickerSelectorView.text = component.surface.rawValue
disabledSwitch.isOn = !component.isEnabled
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)
}
}
extension LabelViewController: ComponentSampleable {
static func makeSample() -> ComponentSample {
let component = Self.makeComponent()
component.text = "Helg¹ worldg Hell¹ worldg"
return ComponentSample(component: component)
}
}
extension BaseViewController {
func descriptionFor(style: TextStyle, useScaledLineHeight: Bool = false, scaledMode: Bool = false) -> String {
let scaledFont = style.font.scaledFont
var customizedDesc: String = "Name: \(style.rawValue) \nFont Face-Weight: \(style.font.fontName)"
customizedDesc += "\nVDS LetterSpacing: \(style.letterSpacing)"
if scaledMode {
customizedDesc += "\nVDS PointSize: \(style.pointSize.rounded())"
customizedDesc += "\n* iOS PointSize: \(scaledFont.pointSize.rounded())"
} else {
customizedDesc += "\n* VDS PointSize: \(style.pointSize.rounded())"
}
if scaledMode {
if useScaledLineHeight {
customizedDesc += "\nVDS LineHeight: \(style.lineHeight)"
customizedDesc += "\n* VDS Ratio LineHeight: \(style.scaledLineHeight)"
} else {
customizedDesc += "\n* VDS LineHeight: \(style.lineHeight)"
}
customizedDesc += "\niOS LineHeight: \(scaledFont.lineHeight.rounded())"
} else {
customizedDesc += "\n* VDS LineHeight: \(style.lineHeight)"
customizedDesc += "\niOS LineHeight: \(style.font.lineHeight.rounded())"
}
return customizedDesc
}
}