vds_ios/VDS/Components/Label/Attributes/TypographicalStyleLabelAttribute.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

87 lines
3.5 KiB
Swift

//
// LabelAttributeFont.swift
// VDS
//
// Created by Matt Bruce on 8/3/22.
//
import Foundation
import UIKit
public struct TypographicalStyleLabelAttribute: LabelAttributeModel {
//--------------------------------------------------
// MARK: - Equatable
//--------------------------------------------------
public func isEqual(_ equatable: TypographicalStyleLabelAttribute) -> Bool {
return id == equatable.id
&& range == equatable.range
&& color == equatable.color
&& style == equatable.style
}
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
public var id = UUID()
public var location: Int
public var length: Int
public var style: TypographicalStyle
public var color: UIColor
public var textPosition: TextPosition
public var lineBreakMode: NSLineBreakMode
//--------------------------------------------------
// MARK: - Initializer
//--------------------------------------------------
public init(location: Int, length: Int, style: TypographicalStyle, color: UIColor = .black, textPosition: TextPosition = .left, lineBreakMode: NSLineBreakMode = .byWordWrapping) {
self.location = location
self.length = length
self.style = style
self.color = color
self.textPosition = textPosition
self.lineBreakMode = lineBreakMode
}
public func setAttribute(on attributedString: NSMutableAttributedString) {
attributedString.removeAttribute(.font, range: range)
attributedString.removeAttribute(.foregroundColor, range: range)
attributedString.addAttribute(.font, value: style.font, range: range)
attributedString.addAttribute(.foregroundColor, value: color, range: range)
setStyleAttributes(attributedString)
}
private func setStyleAttributes(_ attributedString: NSMutableAttributedString) {
//set letterSpacing
if style.letterSpacing > 0.0 {
attributedString.removeAttribute(.kern, range: range)
attributedString.addAttribute(.kern, value: style.letterSpacing, range: range)
}
//set lineHeight
if style.lineHeight > 0.0 {
let lineHeight = style.lineHeight
let adjustment = lineHeight > style.font.lineHeight ? 2.0 : 1.0
let baselineOffset = (lineHeight - style.font.lineHeight) / 2.0 / adjustment
let paragraph = NSMutableParagraphStyle().with {
$0.maximumLineHeight = lineHeight
$0.minimumLineHeight = lineHeight
$0.alignment = textPosition.textAlignment
$0.lineBreakMode = lineBreakMode
}
attributedString.removeAttribute(.baselineOffset, range: range)
attributedString.removeAttribute(.paragraphStyle, range: range)
attributedString.addAttribute(.baselineOffset, value: baselineOffset, range: range)
attributedString.addAttribute(.paragraphStyle, value: paragraph, range: range)
} else if textPosition != .left {
let paragraph = NSMutableParagraphStyle().with {
$0.alignment = textPosition.textAlignment
$0.lineBreakMode = lineBreakMode
}
attributedString.removeAttribute(.paragraphStyle, range: range)
attributedString.addAttribute(.paragraphStyle, value: paragraph, range: range)
}
}
}