also refactored models into class that are the parents Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
80 lines
2.5 KiB
Swift
80 lines
2.5 KiB
Swift
//
|
|
// LabelSystemImageLabelAttribute.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 10/4/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
public struct ImageLabelAttribute: AttachmentLabelAttributeModel {
|
|
//--------------------------------------------------
|
|
// MARK: - Enums
|
|
//--------------------------------------------------
|
|
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?
|
|
|
|
//--------------------------------------------------
|
|
// 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
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Private Functions
|
|
//--------------------------------------------------
|
|
private func imageAttachment(image: UIImage) -> NSTextAttachment {
|
|
let attachment = NSTextAttachment()
|
|
attachment.image = tintColor != nil ? image.withTintColor(tintColor!) : image
|
|
attachment.bounds = frame ?? .init(x: 0, y: 0, width: image.size.width, height: image.size.height)
|
|
return attachment
|
|
}
|
|
|
|
//--------------------------------------------------
|
|
// MARK: - Public Functions
|
|
//--------------------------------------------------
|
|
public func getAttachment() throws -> NSTextAttachment {
|
|
|
|
//get a local asset
|
|
if let imageName {
|
|
guard let bundle = Bundle(identifier: "com.vzw.vds") else {
|
|
throw Error.bundleNotFound
|
|
}
|
|
|
|
guard let image = UIImage(named: imageName, in: bundle, with: nil) 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
|
|
}
|
|
|
|
}
|
|
}
|