vds_ios/VDS/Components/Buttons/TextLinkCaret/TextLinkCaret.swift
Matt Bruce c2d200e7f2 fixed textlinkcaret issue
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-05-08 16:08:04 -05:00

149 lines
4.9 KiB
Swift

//
// TextLinkCaret.swift
// VDS
//
// Created by Matt Bruce on 11/1/22.
//
import Foundation
import UIKit
import VDSColorTokens
import VDSFormControlsTokens
import Combine
@objc(VDSTextLinkCaret)
open class TextLinkCaret: ButtonBase {
//--------------------------------------------------
// MARK: - Enums
//--------------------------------------------------
public enum IconPosition: String, CaseIterable {
case left, right
}
//--------------------------------------------------
// MARK: - Private Properties
//--------------------------------------------------
open override var textStyle: TextStyle {
TextStyle.boldBodyLarge
}
private var imageAttribute: ImageSpaceLabelAttribute?
open override var attributes: [any LabelAttributeModel]? {
guard let imageAttribute else { return nil }
return [imageAttribute]
}
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public override var availableSizes: [ButtonSize] { [.large] }
open var iconPosition: IconPosition = .right { didSet { setNeedsUpdate() } }
private var height: CGFloat {
44
}
open override var textColor: UIColor {
textColorConfiguration.getColor(self)
}
private var textColorConfiguration = ControlColorConfiguration().with {
$0.setSurfaceColors(VDSColor.elementsPrimaryOnlight, VDSColor.elementsPrimaryOndark, forState: .normal)
$0.setSurfaceColors(VDSColor.elementsSecondaryOnlight, VDSColor.elementsSecondaryOndark, forState: .disabled)
$0.setSurfaceColors(VDSColor.interactiveActiveOnlight, VDSColor.interactiveActiveOndark, forState: .highlighted)
}
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
required public init() {
super.init(frame: .zero)
}
public override init(frame: CGRect) {
super.init(frame: .zero)
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
//--------------------------------------------------
// MARK: - Public Functions
//--------------------------------------------------
open override func setup() {
super.setup()
}
open override func reset() {
super.reset()
iconPosition = .right
text = nil
}
//--------------------------------------------------
// MARK: - Overrides
//--------------------------------------------------
override open var intrinsicContentSize: CGSize {
//get the labels size, if not the button
let size = titleLabel?.intrinsicContentSize ?? super.intrinsicContentSize
var itemWidth = size.width
if let caretWidth = imageAttribute?.width {
itemWidth += caretWidth
}
return CGSize(width: itemWidth, height: size.height)
}
open override func updateView() {
let updatedText = text ?? ""
imageAttribute = ImageSpaceLabelAttribute(tintColor: textColor, position: iconPosition)
super.updateView()
}
}
extension TextLinkCaret {
struct ImageSpaceLabelAttribute: LabelAttributeModel {
var id: UUID = .init()
var location: Int = 0
var length: Int = 1
var tintColor: UIColor
var position: IconPosition
var spacerWidth: CGFloat = 2.0
var width: CGFloat { caretSize.width + spacerWidth }
var caretSize: CGSize { Icon.Size.xsmall.dimensions }
init(tintColor: UIColor, position: IconPosition) {
self.tintColor = tintColor
self.position = position
}
func setAttribute(on attributedString: NSMutableAttributedString) {
let imageAttr = ImageLabelAttribute(location: location, imageName: "\(position.rawValue)-caret-bold", frame: .init(x: 0, y: 0, width: caretSize.width, height: caretSize.height), tintColor: tintColor)
let spaceAttr = ImageLabelAttribute(location: 0, imageName: "info", frame: .init(x: 0, y: 0, width: spacerWidth, height: 5.0), tintColor: .clear)
guard let image = try? imageAttr.getAttachment(),
let spacer = try? spaceAttr.getAttachment() else { return }
if position == .right {
attributedString.append(NSAttributedString(attachment: spacer))
attributedString.append(NSAttributedString(attachment: image))
} else {
attributedString.insert(NSAttributedString(attachment: image), at: 0)
attributedString.insert(NSAttributedString(attachment: spacer), at: 1)
}
}
func isEqual(_ equatable: ImageSpaceLabelAttribute) -> Bool {
return id == equatable.id && range == equatable.range
}
}
}