-
Notifications
You must be signed in to change notification settings - Fork 1
/
HexCodeClockView.swift
97 lines (80 loc) · 4.21 KB
/
HexCodeClockView.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// HexCodeClockView.swift
// HexCodeClock
//
// Created by Nasser Eledroos on 10/15/15.
// Copyright © 2015 Nasser Eledroos. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Cocoa
import ScreenSaver
class HexCodeClockView: ScreenSaverView {
let date: NSDate = NSDate()
let formatter: NSDateFormatter = NSDateFormatter()
var datePrefix: String = ""
var timePrefix: String = ""
var time: String = ""
var timeForColor: String = ""
var color: NSColor = NSColor()
override init(frame: NSRect, isPreview: Bool) {
super.init(frame: frame, isPreview: isPreview)!
self.animationTimeInterval = 1
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func startAnimation() {
super.startAnimation()
}
override func stopAnimation() {
super.stopAnimation()
}
override func drawRect(rect: NSRect) {
super.drawRect(rect)
formatter.dateFormat = "EEEE, dd MMMM YYYY"
datePrefix = formatter.stringFromDate(NSDate())
formatter.dateFormat = "HHmmss"
timePrefix = formatter.stringFromDate(NSDate())
formatter.dateFormat = "HHmmss"
timeForColor = formatter.stringFromDate(NSDate())
color = NSColor(hexString: "#\(timeForColor)")! //sets color value to hex from current time
color.set()
NSRectFill(rect)
time = "#\(timePrefix)" // Adds '#' for display
datePrefix.capitalizedString // Capitalizes each first letter
let catTime: NSString = time as NSString // concatenate time as NSString to determineCGSize
let catDate: NSString = datePrefix as NSString // concatenate date as NSString to determine CGSize
let timeSize: CGSize = catTime.sizeWithAttributes([NSFontAttributeName: NSFont(name: "HelveticaNeue-UltraLight", size: 70.0)!])
let dateSize: CGSize = catDate.sizeWithAttributes([NSFontAttributeName: NSFont(name: "HelveticaNeue-UltraLight", size: 25.0)!])
// What the below does, is take half of the dimens. of the time String, and subtract that from half of the frame dimens.
time.drawAtPoint(NSMakePoint((self.frame.size.width/2 - timeSize.width/2), (self.frame.size.height/2 - timeSize.height/8)), withAttributes: [NSFontAttributeName: NSFont(name: "HelveticaNeue-UltraLight", size: 70.0)!, NSForegroundColorAttributeName: NSColor.whiteColor()])
datePrefix.drawAtPoint(NSMakePoint((self.frame.size.width/2 - dateSize.width/2), (self.frame.size.height/2 - dateSize.height)), withAttributes: [NSFontAttributeName: NSFont(name: "HelveticaNeue-UltraLight", size: 25.0)!, NSForegroundColorAttributeName: NSColor.whiteColor()])
}
override func animateOneFrame() {
needsDisplay = true // The subclass lets drawRect: perform the drawing
// in which case animateOneFrame needs to call
// [Obj-C] setNeedsDisplay: with an argument of true
}
override func hasConfigureSheet() -> Bool {
return false
}
override func configureSheet() -> NSWindow? {
return nil
}
}