Files
NetNewsWire/iOS/IconView.swift
T

121 lines
2.8 KiB
Swift
Raw Normal View History

2019-09-17 17:00:23 -05:00
//
// IconView.swift
2019-09-17 17:00:23 -05:00
// NetNewsWire-iOS
//
// Created by Maurice Parker on 9/17/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
2025-11-19 19:33:12 -08:00
import RSCore
2026-05-29 10:46:00 -07:00
import Images
2019-09-17 17:00:23 -05:00
2025-07-23 21:58:07 +08:00
@IBDesignable
final class IconView: UIView {
2019-09-17 17:00:23 -05:00
2026-01-02 15:58:36 -08:00
var iconImage: IconImage? {
2019-09-17 17:00:23 -05:00
didSet {
guard iconImage !== oldValue else {
return
2019-09-17 17:00:23 -05:00
}
imageView.image = iconImage?.image
if iconImage?.isBackgroundSuppressed ?? false {
isDiscernable = true
} else if traitCollection.userInterfaceStyle == .dark {
let isDark = iconImage?.isDark ?? false
isDiscernable = !isDark
2026-01-02 15:58:36 -08:00
} else {
let isBright = iconImage?.isBright ?? false
isDiscernable = !isBright
}
setNeedsLayout()
2019-09-17 17:00:23 -05:00
}
}
2021-03-06 16:25:10 -06:00
private var isDiscernable = true
2019-09-17 17:00:23 -05:00
private let imageView: UIImageView = {
2025-11-19 10:38:18 -08:00
let imageView = NonIntrinsicImageView(image: Assets.Images.faviconTemplate)
2019-09-17 17:00:23 -05:00
imageView.contentMode = .scaleAspectFit
2019-09-26 15:50:05 -05:00
imageView.clipsToBounds = true
2025-07-23 21:58:07 +08:00
imageView.layer.cornerRadius = 2.0
2025-06-13 22:55:37 +08:00
imageView.layer.cornerCurve = .continuous
2019-09-17 17:00:23 -05:00
return imageView
}()
private var isVerticalBackgroundExposed: Bool {
2019-09-17 17:00:23 -05:00
return imageView.frame.size.height < bounds.size.height
}
private var isSymbolImage: Bool {
return iconImage?.isSymbol ?? false
}
2025-11-05 11:22:10 -08:00
private var isBackgroundSuppressed: Bool {
2025-04-28 21:45:23 -07:00
return iconImage?.isBackgroundSuppressed ?? false
}
2025-11-05 11:22:10 -08:00
2019-09-17 17:00:23 -05:00
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
2025-11-05 11:22:10 -08:00
2019-09-17 17:00:23 -05:00
convenience init() {
self.init(frame: .zero)
}
override func didMoveToSuperview() {
setNeedsLayout()
}
override func layoutSubviews() {
imageView.setFrameIfNotEqual(rectForImageView())
updateBackgroundColor()
2019-09-17 17:00:23 -05:00
}
}
private extension IconView {
2019-09-17 17:00:23 -05:00
func commonInit() {
layer.cornerRadius = 4
2019-10-28 20:01:15 -05:00
clipsToBounds = true
2019-09-17 17:00:23 -05:00
addSubview(imageView)
}
func rectForImageView() -> CGRect {
guard !(iconImage?.isSymbol ?? false) else {
return CGRect(x: 0.0, y: 0.0, width: bounds.size.width, height: bounds.size.height)
}
guard let image = iconImage?.image else {
2019-09-17 17:00:23 -05:00
return CGRect.zero
}
let imageSize = image.size
let viewSize = bounds.size
guard imageSize.width > 0.0, imageSize.height > 0.0 else {
return CGRect.zero
}
// Aspect-fit, but never scale up — small icons render at natural size, centered.
let factor = min(viewSize.width / imageSize.width, viewSize.height / imageSize.height, 1.0)
let width = imageSize.width * factor
2019-09-17 17:00:23 -05:00
let height = imageSize.height * factor
let originX = floor((viewSize.width - width) / 2.0)
2019-09-17 17:00:23 -05:00
let originY = floor((viewSize.height - height) / 2.0)
return CGRect(x: originX, y: originY, width: width, height: height)
2019-09-17 17:00:23 -05:00
}
private func updateBackgroundColor() {
if !isBackgroundSuppressed && ((iconImage != nil && isVerticalBackgroundExposed) || !isDiscernable) {
2025-11-19 10:38:18 -08:00
backgroundColor = Assets.Colors.iconBackground
} else {
backgroundColor = nil
}
}
2019-09-17 17:00:23 -05:00
}