90 lines
3.6 KiB
Swift
90 lines
3.6 KiB
Swift
//
|
|
// LabelAttributeFont.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 8/3/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
public struct TextStyleLabelAttribute: LabelAttributeModel {
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Equatable
|
|
//--------------------------------------------------
|
|
public func isEqual(_ equatable: TextStyleLabelAttribute) -> Bool {
|
|
return id == equatable.id
|
|
&& range == equatable.range
|
|
&& textColor == equatable.textColor
|
|
&& textStyle == equatable.textStyle
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Properties
|
|
//--------------------------------------------------
|
|
public var id = UUID()
|
|
public var location: Int
|
|
public var length: Int
|
|
public var textStyle: TextStyle
|
|
public var textColor: UIColor?
|
|
public var textPosition: TextAlignment
|
|
public var lineBreakMode: NSLineBreakMode
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Initializer
|
|
//--------------------------------------------------
|
|
public init(location: Int, length: Int, textStyle: TextStyle, textColor: UIColor? = nil, textPosition: TextAlignment = .left, lineBreakMode: NSLineBreakMode = .byWordWrapping) {
|
|
self.location = location
|
|
self.length = length
|
|
self.textStyle = textStyle
|
|
self.textColor = textColor
|
|
self.textPosition = textPosition
|
|
self.lineBreakMode = lineBreakMode
|
|
}
|
|
|
|
public func setAttribute(on attributedString: NSMutableAttributedString) {
|
|
guard isValidRange(on: attributedString) else { return }
|
|
attributedString.removeAttribute(.font, range: range)
|
|
attributedString.addAttribute(.font, value: textStyle.font, range: range)
|
|
if let textColor {
|
|
attributedString.removeAttribute(.foregroundColor, range: range)
|
|
attributedString.addAttribute(.foregroundColor, value: textColor, range: range)
|
|
}
|
|
setStyleAttributes(attributedString)
|
|
}
|
|
|
|
private func setStyleAttributes(_ attributedString: NSMutableAttributedString) {
|
|
//set letterSpacing
|
|
if textStyle.letterSpacing > 0.0 {
|
|
attributedString.removeAttribute(.kern, range: range)
|
|
attributedString.addAttribute(.kern, value: textStyle.letterSpacing, range: range)
|
|
}
|
|
|
|
//set lineHeight
|
|
if textStyle.lineHeight > 0.0 {
|
|
let lineHeight = textStyle.lineHeight
|
|
let adjustment = lineHeight > textStyle.font.lineHeight ? 2.0 : 1.0
|
|
let baselineOffset = (lineHeight - textStyle.font.lineHeight) / 2.0 / adjustment
|
|
let paragraph = NSMutableParagraphStyle().with {
|
|
$0.maximumLineHeight = lineHeight
|
|
$0.minimumLineHeight = lineHeight
|
|
$0.alignment = textPosition.value
|
|
$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.value
|
|
$0.lineBreakMode = lineBreakMode
|
|
}
|
|
attributedString.removeAttribute(.paragraphStyle, range: range)
|
|
attributedString.addAttribute(.paragraphStyle, value: paragraph, range: range)
|
|
}
|
|
}
|
|
}
|