| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- //
- // IndeedLogoView.swift
- // App for Indeed
- //
- import Cocoa
- /// Indeed wordmark (`indeed` in brand blue) for splash and branding surfaces.
- enum IndeedBrandLogo {
- static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
- static func wordmarkImage(fittingHeight height: CGFloat) -> NSImage {
- let fontSize = height * 0.92
- let font = NSFont.systemFont(ofSize: fontSize, weight: .heavy)
- let text = "indeed" as NSString
- let attributes: [NSAttributedString.Key: Any] = [
- .font: font,
- .foregroundColor: brandBlue,
- .kern: -0.6
- ]
- let size = text.size(withAttributes: attributes)
- let canvas = NSSize(width: ceil(size.width) + 4, height: ceil(size.height) + 4)
- let image = NSImage(size: canvas)
- image.lockFocus()
- defer { image.unlockFocus() }
- NSColor.clear.set()
- NSRect(origin: .zero, size: canvas).fill()
- let origin = NSPoint(x: 2, y: (canvas.height - size.height) / 2)
- text.draw(at: origin, withAttributes: attributes)
- return image
- }
- }
- final class IndeedLogoView: NSView {
- private let imageView = NSImageView()
- private var displayHeight: CGFloat = 44
- override var intrinsicContentSize: NSSize {
- guard let image = imageView.image, image.size.height > 0 else {
- return NSSize(width: 140, height: displayHeight)
- }
- let aspect = image.size.width / image.size.height
- return NSSize(width: displayHeight * aspect, height: displayHeight)
- }
- init(displayHeight: CGFloat = 44) {
- self.displayHeight = displayHeight
- super.init(frame: .zero)
- setUp()
- }
- @available(*, unavailable)
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- func setDisplayHeight(_ height: CGFloat) {
- displayHeight = height
- refreshImage()
- invalidateIntrinsicContentSize()
- }
- private func setUp() {
- translatesAutoresizingMaskIntoConstraints = false
- imageView.translatesAutoresizingMaskIntoConstraints = false
- imageView.imageScaling = .scaleProportionallyUpOrDown
- addSubview(imageView)
- NSLayoutConstraint.activate([
- imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
- imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
- imageView.topAnchor.constraint(equalTo: topAnchor),
- imageView.bottomAnchor.constraint(equalTo: bottomAnchor)
- ])
- refreshImage()
- setAccessibilityElement(true)
- setAccessibilityRole(.image)
- setAccessibilityLabel("Indeed")
- }
- private func refreshImage() {
- imageView.image = IndeedBrandLogo.wordmarkImage(fittingHeight: displayHeight)
- }
- }
|