|
| 1 | +// |
| 2 | +// TextStyleLabel.swift |
| 3 | +// YMatterType |
| 4 | +// |
| 5 | +// Created by Virginia Pujols on 1/11/23. |
| 6 | +// Copyright © 2023 Y Media Labs. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +public struct TextStyleLabel: View { |
| 12 | + private let text: String |
| 13 | + private let typography: Typography |
| 14 | + private let configureTypographyText: ((TypographyLabel) -> Void)? |
| 15 | + |
| 16 | + public init(_ text: String, |
| 17 | + typography: Typography, |
| 18 | + configuration: ((TypographyLabel) -> Void)? = nil) { |
| 19 | + self.typography = typography |
| 20 | + self.text = text |
| 21 | + self.configureTypographyText = configuration |
| 22 | + } |
| 23 | + |
| 24 | + public var body: some View { |
| 25 | + TypographyLabelRepresentable(text, |
| 26 | + typography: typography, |
| 27 | + configuration: configureTypographyText) |
| 28 | + .fixedSize(horizontal: false, vertical: true) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +public extension TextStyleLabel { |
| 33 | + struct TypographyLabelRepresentable: UIViewRepresentable { |
| 34 | + public typealias UIViewType = TypographyLabel |
| 35 | + |
| 36 | + private let typography: Typography |
| 37 | + private let text: String |
| 38 | + private let configureText: ((TypographyLabel) -> Void)? |
| 39 | + |
| 40 | + public init(_ text: String, |
| 41 | + typography: Typography, |
| 42 | + configuration: ((TypographyLabel) -> Void)? = nil) { |
| 43 | + self.typography = typography |
| 44 | + self.text = text |
| 45 | + self.configureText = configuration |
| 46 | + } |
| 47 | + |
| 48 | + public func makeUIView(context: Context) -> TypographyLabel { |
| 49 | + let label = TypographyLabel(typography: typography) |
| 50 | + label.text = text |
| 51 | + |
| 52 | + label.numberOfLines = 1 |
| 53 | + |
| 54 | + label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) |
| 55 | + label.setContentCompressionResistancePriority(.defaultLow, for: .vertical) |
| 56 | + |
| 57 | + configureText?(label) |
| 58 | + return label |
| 59 | + } |
| 60 | + |
| 61 | + public func updateUIView(_ uiView: TypographyLabel, context: Context) { |
| 62 | + uiView.typography = typography |
| 63 | + uiView.text = text |
| 64 | + configureText?(uiView) |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments