vds_ios/VDS/Components/Label/Attributes/UnderlineLabelAttribute.swift
Matt Bruce 669e0d7625 refactored enums into class/structs that use them
also refactored models into class that are the parents

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2023-01-13 13:32:38 -06:00

117 lines
3.5 KiB
Swift

//
// LabelAttributeUnderline.swift
// VDS
//
// Created by Matt Bruce on 8/3/22.
//
import Foundation
import UIKit
public struct UnderlineLabelAttribute: LabelAttributeModel {
//--------------------------------------------------
// MARK: - Equatable
//--------------------------------------------------
public func isEqual(_ equatable: UnderlineLabelAttribute) -> Bool {
return id == equatable.id
&& range == equatable.range
&& color == equatable.color
&& style == equatable.style
&& pattern == equatable.pattern
}
//--------------------------------------------------
// MARK: - Public Properties
//--------------------------------------------------
public var id = UUID()
public var location: Int
public var length: Int
public var color: UIColor?
public var style: Style = .single
public var pattern: Pattern?
public var underlineValue: NSUnderlineStyle {
if let pattern = pattern?.value() {
return NSUnderlineStyle(rawValue: style.value() | pattern)
} else {
return NSUnderlineStyle(rawValue: style.value())
}
}
//--------------------------------------------------
// MARK: - Initializers
//--------------------------------------------------
public init(location: Int, length: Int, style: Style = .single, color: UIColor? = nil, pattern: Pattern? = nil) {
self.location = location
self.length = length
self.color = color
self.style = style
self.pattern = pattern
}
//--------------------------------------------------
// MARK: - Public Functions
//--------------------------------------------------
public func setAttribute(on attributedString: NSMutableAttributedString) {
attributedString.addAttribute(.underlineStyle, value: underlineValue.rawValue, range: range)
if let color = color {
attributedString.addAttribute(.underlineColor, value: color, range: range)
}
}
}
extension UnderlineLabelAttribute {
//--------------------------------------------------
// MARK: - Enums
//--------------------------------------------------
public enum Style: String, Codable {
case none
case single
case thick
case double
func value() -> Int {
switch self {
case .none:
return 0
case .single:
return NSUnderlineStyle.single.rawValue
case .thick:
return NSUnderlineStyle.thick.rawValue
case .double:
return NSUnderlineStyle.double.rawValue
}
}
}
public enum Pattern: String, Codable {
case dot
case dash
case dashDot
case dashDotDot
case byWord
func value() -> Int {
switch self {
case .dot:
return NSUnderlineStyle.patternDot.rawValue
case .dash:
return NSUnderlineStyle.patternDash.rawValue
case .dashDot:
return NSUnderlineStyle.patternDashDot.rawValue
case .dashDotDot:
return NSUnderlineStyle.patternDashDotDot.rawValue
case .byWord:
return NSUnderlineStyle.byWord.rawValue
}
}
}
}