96 lines
3.1 KiB
Swift
96 lines
3.1 KiB
Swift
//
|
|
// LabelSystemImageLabelAttribute.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 10/4/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
public struct ImageLabelAttribute: AttachmentLabelAttributeModel {
|
|
//--------------------------------------------------
|
|
// MARK: - Enums
|
|
//--------------------------------------------------
|
|
/// Enum used to describe the type of error that can occur.
|
|
public enum Error: Swift.Error {
|
|
case bundleNotFound
|
|
case imageNotFound(String)
|
|
case imageNotSet
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Properties
|
|
//--------------------------------------------------
|
|
public var id = UUID()
|
|
public var location: Int
|
|
public var length: Int = 1
|
|
public var imageName: String?
|
|
public var image: UIImage?
|
|
public var frame: CGRect?
|
|
public var tintColor: UIColor?
|
|
public var accessibleText: String?
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Equatable
|
|
//--------------------------------------------------
|
|
public static func == (lhs: ImageLabelAttribute, rhs: ImageLabelAttribute) -> Bool {
|
|
lhs.isEqual(rhs)
|
|
}
|
|
|
|
public func isEqual(_ equatable: ImageLabelAttribute) -> Bool {
|
|
return id == equatable.id && range == equatable.range && imageName == equatable.imageName
|
|
}
|
|
|
|
public init(id: UUID = UUID(), location: Int, imageName: String? = nil, image: UIImage? = nil, frame: CGRect? = nil, tintColor: UIColor? = nil, accessibleText: String? = nil) {
|
|
self.id = id
|
|
self.location = location
|
|
self.imageName = imageName
|
|
self.image = image
|
|
self.frame = frame
|
|
self.tintColor = tintColor
|
|
self.accessibleText = accessibleText
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Methods
|
|
//--------------------------------------------------
|
|
private func imageAttachment(image: UIImage) -> NSTextAttachment {
|
|
let attachment = NSTextAttachment()
|
|
if let accessibleText {
|
|
attachment.accessibilityLabel = accessibleText
|
|
attachment.isAccessibilityElement = true
|
|
} else {
|
|
attachment.isAccessibilityElement = false
|
|
}
|
|
attachment.image = tintColor != nil ? image.withTintColor(tintColor!) : image
|
|
if let frame {
|
|
attachment.bounds = frame
|
|
}
|
|
return attachment
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Methods
|
|
//--------------------------------------------------
|
|
public func getAttachment() throws -> NSTextAttachment {
|
|
|
|
//get a local asset
|
|
if let imageName {
|
|
guard let image = BundleManager.shared.image(for: imageName) else {
|
|
throw Error.imageNotFound(imageName)
|
|
}
|
|
|
|
return imageAttachment(image: image)
|
|
|
|
} //get from set image
|
|
else if let image {
|
|
return imageAttachment(image: image)
|
|
|
|
} else {
|
|
throw Error.imageNotSet
|
|
}
|
|
|
|
}
|
|
}
|