vds_ios/VDS/Components/Label/Attributes/LabelAttributeUnderline.swift
Matt Bruce 378802eb5e updated to equalSelf
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-09-15 16:50:21 -05:00

102 lines
2.7 KiB
Swift

//
// LabelAttributeUnderline.swift
// VDS
//
// Created by Matt Bruce on 8/3/22.
//
import Foundation
import UIKit
public struct LabelAttributeUnderline: LabelAttributeModel {
public func isEqualSelf(_ equatable: LabelAttributeUnderline) -> Bool {
return id == equatable.id
&& range == equatable.range
&& color == equatable.color
&& style == equatable.style
&& pattern == equatable.pattern
}
public var id = UUID()
public var location: Int
public var length: Int
public var color: UIColor?
public var style: UnderlineStyle = .single
public var pattern: UnderlineStyle.Pattern?
public var underlineValue: NSUnderlineStyle {
if let pattern = pattern?.value() {
return NSUnderlineStyle(rawValue: style.value() | pattern)
} else {
return NSUnderlineStyle(rawValue: style.value())
}
}
public init(location: Int, length: Int, style: UnderlineStyle = .single, color: UIColor = .black, pattern: UnderlineStyle.Pattern? = nil) {
self.location = location
self.length = length
self.color = color
self.style = style
self.pattern = pattern
}
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)
}
}
}
public enum UnderlineStyle: 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
}
}
}
}