vds_ios/VDS/Components/Label/Attributes/ImageLabelAttribute.swift
Matt Bruce 26da08ba48 images
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-10-07 09:13:20 -05:00

45 lines
1.3 KiB
Swift

//
// LabelSystemImageLabelAttribute.swift
// VDS
//
// Created by Matt Bruce on 10/4/22.
//
import Foundation
import UIKit
public struct ImageLabelAttribute: AttachmentLabelAttributeModel {
public enum Error: Swift.Error {
case bundleNotFound
case imageNotFound(String)
}
public var id = UUID()
public var location: Int
public var length: Int
public var imageName: String
public var frame: CGRect
public var tintColor: UIColor
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 func getAttachment() throws -> NSTextAttachment {
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)
}
let attachment = NSTextAttachment()
attachment.image = image.withTintColor(tintColor)
attachment.bounds = frame
return attachment
}
}