refactored for the iconName image helper

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-03-12 09:57:42 -05:00
parent 42dccb4780
commit 8369403cbe

View File

@ -44,7 +44,7 @@ open class Icon: View {
//--------------------------------------------------
/// UIImageView used to render the icon.
open var imageView = UIImageView().with {
$0.isAccessibilityElement = false
$0.isAccessibilityElement = false
$0.translatesAutoresizingMaskIntoConstraints = false
$0.contentMode = .scaleAspectFill
$0.clipsToBounds = true
@ -109,8 +109,8 @@ open class Icon: View {
//get the image name
//set the image
if let name, let image = getImage(for: name.rawValue) {
setImage(image: image, imageColor: imageColor)
if let name, let image = UIImage.image(for: name, color: imageColor) {
imageView.image = image
} else {
imageView.image = nil
}
@ -129,17 +129,17 @@ open class Icon: View {
super.updateAccessibility()
accessibilityLabel = name?.rawValue ?? "icon"
}
//--------------------------------------------------
// MARK: - Private Methods
//--------------------------------------------------
private func getImage(for imageName: String) -> UIImage? {
return BundleManager.shared.image(for: imageName)
}
private func setImage(image: UIImage, imageColor: UIColor) {
imageView.image = image.withTintColor(imageColor)
}
}
extension UIImage {
/// UIImage helper for finding images based on the Icon.Name which uses the internal BundleManager.
/// - Parameters:
/// - name: Icon.Name rawRepresentable.
/// - color: Color to Tint the image with
/// - renderingMode: UIImage Rendering mode.
/// - Returns: UIImage for this proecess
public static func image(for iconName: Icon.Name, color: UIColor? = nil, renderingMode: UIImage.RenderingMode = .alwaysOriginal) -> UIImage? {
image(representing: iconName, color: color, renderingMode: renderingMode)
}
}