-
Notifications
You must be signed in to change notification settings - Fork 0
/
BadgerHardcoded.swift
executable file
·67 lines (50 loc) · 1.68 KB
/
BadgerHardcoded.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#! /usr/bin/swift
import AppKit
extension NSImage {
convenience init?(url: URL) {
guard let iconData = try? Data(contentsOf: url) else {
return nil
}
self.init(data: iconData)
}
var asPNGData: Data? {
guard let imageData = tiffRepresentation,
let newRep = NSBitmapImageRep(data: imageData) else {
return nil
}
return newRep.representation(using: .png, properties: [:])
}
}
func addBadge(_ text: String, imageSize: CGSize, with color: NSColor) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
paragraphStyle.lineBreakMode = .byWordWrapping
let attributes: [NSAttributedString.Key: Any] = [
.font: NSFont.init(name: "SFProRounded-Medium", size: 120)!,
.paragraphStyle: paragraphStyle,
.foregroundColor: color
]
let textRect = NSRect(x: 0, y: imageSize.height * 3/4, width: imageSize.width, height: imageSize.height * 1/4)
NSColor(red: 0.06, green: 0.13, blue: 0.27, alpha: 1.00).drawSwatch(in: textRect)
text.draw(
with: textRect,
options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine],
attributes: attributes
)
}
let url = URL(filePath: FileManager.default.currentDirectoryPath + "/Icon.png")
guard let iconImage = NSImage(url: url) else {
print("Error: icon not found")
exit(1)
}
let badgedIcon = NSImage(size: iconImage.size, flipped: true) { rect -> Bool in
iconImage.draw(in: rect)
addBadge("Croissant", imageSize: iconImage.size, with: .white)
return true
}
guard let pngData = badgedIcon.asPNGData else {
print("Error: couldn't convert image to png data")
exit(1)
}
let outputURL = URL(filePath: FileManager.default.currentDirectoryPath + "/BadgedIcon.png")
try pngData.write(to: outputURL)