Нет описания

IndeedLogoView.swift 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // IndeedLogoView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. /// Indeed wordmark (`indeed` in brand blue) for splash and branding surfaces.
  7. enum IndeedBrandLogo {
  8. static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  9. static func wordmarkImage(fittingHeight height: CGFloat) -> NSImage {
  10. let fontSize = height * 0.92
  11. let font = NSFont.systemFont(ofSize: fontSize, weight: .heavy)
  12. let text = "indeed" as NSString
  13. let attributes: [NSAttributedString.Key: Any] = [
  14. .font: font,
  15. .foregroundColor: brandBlue,
  16. .kern: -0.6
  17. ]
  18. let size = text.size(withAttributes: attributes)
  19. let canvas = NSSize(width: ceil(size.width) + 4, height: ceil(size.height) + 4)
  20. let image = NSImage(size: canvas)
  21. image.lockFocus()
  22. defer { image.unlockFocus() }
  23. NSColor.clear.set()
  24. NSRect(origin: .zero, size: canvas).fill()
  25. let origin = NSPoint(x: 2, y: (canvas.height - size.height) / 2)
  26. text.draw(at: origin, withAttributes: attributes)
  27. return image
  28. }
  29. }
  30. final class IndeedLogoView: NSView {
  31. private let imageView = NSImageView()
  32. private var displayHeight: CGFloat = 44
  33. override var intrinsicContentSize: NSSize {
  34. guard let image = imageView.image, image.size.height > 0 else {
  35. return NSSize(width: 140, height: displayHeight)
  36. }
  37. let aspect = image.size.width / image.size.height
  38. return NSSize(width: displayHeight * aspect, height: displayHeight)
  39. }
  40. init(displayHeight: CGFloat = 44) {
  41. self.displayHeight = displayHeight
  42. super.init(frame: .zero)
  43. setUp()
  44. }
  45. @available(*, unavailable)
  46. required init?(coder: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. func setDisplayHeight(_ height: CGFloat) {
  50. displayHeight = height
  51. refreshImage()
  52. invalidateIntrinsicContentSize()
  53. }
  54. private func setUp() {
  55. translatesAutoresizingMaskIntoConstraints = false
  56. imageView.translatesAutoresizingMaskIntoConstraints = false
  57. imageView.imageScaling = .scaleProportionallyUpOrDown
  58. addSubview(imageView)
  59. NSLayoutConstraint.activate([
  60. imageView.leadingAnchor.constraint(equalTo: leadingAnchor),
  61. imageView.trailingAnchor.constraint(equalTo: trailingAnchor),
  62. imageView.topAnchor.constraint(equalTo: topAnchor),
  63. imageView.bottomAnchor.constraint(equalTo: bottomAnchor)
  64. ])
  65. refreshImage()
  66. setAccessibilityElement(true)
  67. setAccessibilityRole(.image)
  68. setAccessibilityLabel("Indeed")
  69. }
  70. private func refreshImage() {
  71. imageView.image = IndeedBrandLogo.wordmarkImage(fittingHeight: displayHeight)
  72. }
  73. }