92 lines
2.4 KiB
Swift
92 lines
2.4 KiB
Swift
//
|
|
// LabelAttributeUnderline.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 8/3/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
public struct LabelAttributeUnderline: LabelAttributeModel {
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|