iOS/Sources/Shared/Iconic/IconImageView.swift
Zac West 640cca884b
Add SwiftFormat to project (#1463)
Adds new fastlane lanes:
- `fastlane lint` which checks the linters
- `fastlane autocorrect` which applies the linters which can autocorrect (Rubocop, SwiftFormat)

Adds a build step to the Codegen abstract target which runs SwiftFormat in lint mode, pointing out what it's going to change when run.

Applies SwiftFormat to nearly all code -- exempts a few externally-sourced files and generated code.
2021-02-05 22:06:25 -08:00

62 lines
1.4 KiB
Swift

// swiftlint:disable all
// swiftformat:disable all
//
// IconImageView.swift
// https://github.com/home-assistant/Iconic
//
// Copyright © 2019 The Home Assistant Authors
// Licensed under the Apache 2.0 license
// For more information see https://github.com/home-assistant/Iconic
//
import UIKit
#if os(iOS) || os(tvOS)
/** An Image View subclass, capable of rendering icons. Only supported for iOS and tvOS. */
public class IconImageView: UIImageView {
// MARK: - Public Variables
/** The icon drawable to be used as image. */
public var iconDrawable: IconDrawable? {
didSet {
updateIconImage()
}
}
// MARK: - Overrides
public override var frame: CGRect {
didSet {
updateIconImage()
}
}
public override func layoutSubviews() {
super.layoutSubviews()
updateIconImage()
}
// MARK: - Image Constructor
/**
Updates the icon image, only when the frame is not empty.
*/
fileprivate func updateIconImage() {
// No need to update the icon with empty frame
if frame.isEmpty {
return
}
if let icon = iconDrawable {
let image = icon.image(ofSize: frame.size, color: nil)
self.image = image.withRenderingMode(.alwaysTemplate)
} else {
self.image = nil
}
}
}
#endif