Digital ACT-191 ONEAPP-9314 story: applying textstyle and color

This commit is contained in:
Vasavi Kanamarlapudi 2024-08-13 15:37:02 +05:30
parent b1adbd7e0d
commit 1bb54d174f

View File

@ -68,6 +68,8 @@ open class PriceLockup: View {
case large
case xlarge
case xxlarge
public var defaultValue: Self { .medium }
}
//--------------------------------------------------
@ -111,7 +113,6 @@ open class PriceLockup: View {
/// This will render the pricing and term sections as a uniform size.
open var uniformSize: Bool = false { didSet { setNeedsUpdate() } }
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
@ -124,25 +125,79 @@ open class PriceLockup: View {
$0.lineBreakMode = .byWordWrapping
}
internal var index = 0
internal var textContentType:TextContentType = .preDelimiter
enum TextContentType: String, CaseIterable {
case preDelimiter, postDelimiter
}
//--------------------------------------------------
// MARK: - Configuration Properties
//--------------------------------------------------
internal var containerSize: CGSize { CGSize(width: 45, height: 44) }
private var kindColorConfiguration: AnyColorable {
switch kind {
case .primary:
return ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)}.eraseToAnyColorable()
case .secondary:
return ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark, forState: .normal)}.eraseToAnyColorable()
case .savings:
return ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.paletteGreen26, VDSColor.paletteGreen36, forState: .normal)}.eraseToAnyColorable()
private var contentFontSize: VDS.TextStyle {
switch (size, textContentType) {
case (.xxxsmall, .preDelimiter), (.xxxsmall, .postDelimiter):
return TextStyle.micro
case (.xxsmall, .preDelimiter), (.xxsmall, .postDelimiter):
return TextStyle.bodySmall
case (.xsmall, .preDelimiter), (.xsmall, .postDelimiter):
return TextStyle.bodyMedium
case (.small, .preDelimiter), (.small, .postDelimiter):
return TextStyle.bodyLarge
case (.medium, .preDelimiter):
return UIDevice.isIPad ? TextStyle.titleSmall : TextStyle.titleMedium
case (.medium, .postDelimiter):
return TextStyle.bodyLarge
case (.large, .preDelimiter):
return UIDevice.isIPad ? TextStyle.titleMedium : TextStyle.titleLarge
case (.large, .postDelimiter):
return UIDevice.isIPad ? TextStyle.titleSmall : TextStyle.titleMedium
case (.xlarge, .preDelimiter):
return UIDevice.isIPad ? TextStyle.titleLarge : TextStyle.titleXLarge
case (.xlarge, .postDelimiter):
return UIDevice.isIPad ? TextStyle.titleMedium : TextStyle.titleLarge
case (.xxlarge, .preDelimiter):
return UIDevice.isIPad ? TextStyle.titleXLarge : TextStyle.featureSmall
case (.xxlarge, .postDelimiter):
return UIDevice.isIPad ? TextStyle.titleLarge : TextStyle.titleXLarge
}
}
private var kindViewColorConfiguration: ViewColorConfiguration {
switch kind {
case .primary:
return ViewColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forDisabled: false)
}
case .secondary:
return ViewColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark, forDisabled: false)
}
case .savings:
return ViewColorConfiguration().with {
$0.setSurfaceColors(VDSColor.paletteGreen26, VDSColor.paletteGreen36, forDisabled: false)
}
}
}
private var heightConstraint: NSLayoutConstraint?
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
@ -163,12 +218,51 @@ open class PriceLockup: View {
containerView.addSubview(label)
label.pinToSuperView()
label.centerXAnchor.constraint(equalTo: centerXAnchor).activate()
heightConstraint = label.heightAnchor.constraint(equalToConstant: 0)
heightConstraint?.activate()
}
func updateLabel() {
var attributes: [any LabelAttributeModel] = []
let colorAttr = ColorLabelAttribute(location: 0,
length: label.text.count,
color: kindViewColorConfiguration.getColor(self))
attributes.append(colorAttr)
if index > 0 {
textContentType = .preDelimiter
let textStyleAttr = TextStyleLabelAttribute(location: 0,
length: index,
textStyle: contentFontSize)
textContentType = .postDelimiter
let othertextStyleAttr = TextStyleLabelAttribute(location: index+1,
length: label.text.count-index-1,
textStyle: contentFontSize)
attributes.append(textStyleAttr)
attributes.append(othertextStyleAttr)
}
label.attributes = attributes
}
/// Used to make changes to the View based off a change events or from local properties.
open override func updateView() {
super.updateView()
label.text = fetchText()
label.surface = surface
// Set the attributed text
updateLabel()
if uniformSize {
// currency and value have the same font text style as delimeter, term, trailing text and superscript.
textContentType = .postDelimiter
var uniformSizeAttributes: [any LabelAttributeModel]? {
[TextStyleLabelAttribute(location: 0,
length: label.text.count,
textStyle: contentFontSize)]
}
label.attributes = uniformSizeAttributes
}
}
/// Resets to default settings.
@ -192,12 +286,16 @@ open class PriceLockup: View {
//--------------------------------------------------
open func fetchText() -> String {
var text : String = ""
index = 0
let currency: String = hideCurrency ? "" : "$"
if let leadingStr = leadingText {
text = text + leadingStr + " "
index = index + leadingStr.count + 1
}
if let value = price {
text = text + currency + "\(value)"
let valueStr = "\(value)"
text = text + currency + valueStr
index = index + valueStr.count + 1
}
if term != .none {
text = text + "/" + term.text