2 // GuardEscapedView.swift
5 // Created by Hori,Masaki on 2017/01/03.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 class GuardEscapedView: NSView {
12 private static var taihiStrings: String = {
13 guard let url = Bundle.main.url(forResource: "Taihi", withExtension: "txt"),
14 let taihiString = try? String(contentsOf: url, encoding: .utf8)
15 else { fatalError("Can not load Taihi.txt") }
16 guard (taihiString as NSString).length == 2 else { fatalError("Taihi string is not 2 charactor") }
20 var controlSize: NSControlSize = .regular
21 private var taiString: String {
22 let s = GuardEscapedView.taihiStrings
23 let range = s.index(s.startIndex, offsetBy: 0)
24 return String(s[range])
26 private var hiString: String {
27 let s = GuardEscapedView.taihiStrings
28 let range = s.index(s.endIndex, offsetBy: -1)
29 return String(s[range])
32 override func draw(_ dirtyRect: NSRect) {
35 let bounds = self.bounds
36 NSColor(calibratedWhite: 0.9, alpha: 0.8).setFill()
37 NSBezierPath(rect: bounds).fill()
40 case .regular: drawTaihi(in: bounds)
41 case .small, .mini: drawSmallTaihi(in: bounds)
45 private func drawTaihi(in bounds: NSRect) {
46 let rotate = NSAffineTransform()
47 rotate.translateX(by: 0.0, yBy: 65.0)
48 rotate.rotate(byDegrees: -27)
53 let x = Int((bounds.width - CGFloat(width)) * 0.5)
54 let y = Int((bounds.height - CGFloat(height)) * 0.5)
55 let rect = NSRect(x: x, y: y, width: width, height: height)
56 let path = NSBezierPath(rect: rect)
57 NSColor.white.setFill()
58 NSColor.black.setStroke()
62 let borderRect = rect.insetBy(dx: 3, dy: 3)
63 let borderPath = NSBezierPath(rect: borderRect)
64 borderPath.lineWidth = 2.0
67 let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(width - 10))
68 let attributes = [ NSForegroundColorAttributeName: NSColor.lightGray,
69 NSFontAttributeName: fontSize ]
70 let tai = NSAttributedString(string: taiString, attributes: attributes)
71 let hi = NSAttributedString(string: hiString, attributes: attributes)
73 var stringRect = borderRect.insetBy(dx: 2, dy: 2)
74 stringRect.origin.y += 4
75 stringRect.origin.x += 1.5
76 stringRect.size.height -= 2
77 tai.draw(in: stringRect)
78 stringRect.size.height *= 0.5
79 hi.draw(in: stringRect)
81 private func drawSmallTaihi(in bounds: NSRect) {
84 let x = Int((bounds.width - CGFloat(width)) * 0.5)
85 let y = Int((bounds.height - CGFloat(height)) * 0.5)
86 let rect = NSRect(x: x, y: y, width: width, height: height)
87 let path = NSBezierPath(rect: rect)
88 NSColor.white.setFill()
89 NSColor.black.setStroke()
93 let borderRect = rect.insetBy(dx: 3, dy: 3)
94 let borderPath = NSBezierPath(rect: borderRect)
95 borderPath.lineWidth = 2.0
98 let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(height - 14))
99 let attributes = [ NSForegroundColorAttributeName: NSColor.lightGray,
100 NSFontAttributeName: fontSize ]
101 let tai = NSAttributedString(string: taiString, attributes: attributes)
102 let hi = NSAttributedString(string: hiString, attributes: attributes)
104 var stringRect = borderRect.insetBy(dx: 2, dy: 2)
105 stringRect.origin.y += 5
106 stringRect.origin.x += 5
107 stringRect.size.height -= 2
108 tai.draw(in: stringRect)
109 stringRect.origin.x += stringRect.width * 0.5 + 1
110 hi.draw(in: stringRect)