2 // GuardEscapedView.swift
5 // Created by Hori,Masaki on 2017/01/03.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 final class GuardEscapedView: NSView {
13 private static var taihiStrings: String = {
15 guard let url = Bundle.main.url(forResource: "Taihi", withExtension: "txt"),
16 let taihiString = try? String(contentsOf: url, encoding: .utf8)
17 else { fatalError("Can not load Taihi.txt") }
19 guard (taihiString as NSString).length == 2 else { fatalError("Taihi string is not 2 charactor") }
24 var controlSize: NSControl.ControlSize = .regular
25 private var taiString: String {
27 let s = GuardEscapedView.taihiStrings
28 let range = s.index(s.startIndex, offsetBy: 0)
30 return String(s[range])
33 private var hiString: String {
35 let s = GuardEscapedView.taihiStrings
36 let range = s.index(s.endIndex, offsetBy: -1)
38 return String(s[range])
41 override func draw(_ dirtyRect: NSRect) {
45 let bounds = self.bounds
46 NSColor(calibratedWhite: 0.9, alpha: 0.8).setFill()
47 NSBezierPath(rect: bounds).fill()
50 case .regular: drawTaihi(in: bounds)
51 case .small, .mini: drawSmallTaihi(in: bounds)
55 private func drawTaihi(in bounds: NSRect) {
57 let rotate = NSAffineTransform()
58 rotate.translateX(by: 0.0, yBy: 65.0)
59 rotate.rotate(byDegrees: -27)
64 let x = Int((bounds.width - CGFloat(width)) * 0.5)
65 let y = Int((bounds.height - CGFloat(height)) * 0.5)
66 let rect = NSRect(x: x, y: y, width: width, height: height)
67 let path = NSBezierPath(rect: rect)
68 NSColor.white.setFill()
69 NSColor.black.setStroke()
73 let borderRect = rect.insetBy(dx: 3, dy: 3)
74 let borderPath = NSBezierPath(rect: borderRect)
75 borderPath.lineWidth = 2.0
78 let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(width - 10))
79 let attributes = [ NSAttributedStringKey.foregroundColor: NSColor.lightGray,
80 NSAttributedStringKey.font: fontSize ]
81 let tai = NSAttributedString(string: taiString, attributes: attributes)
82 let hi = NSAttributedString(string: hiString, attributes: attributes)
84 var stringRect = borderRect.insetBy(dx: 2, dy: 2)
85 stringRect.origin.y += 4
86 stringRect.origin.x += 1.5
87 stringRect.size.height -= 2
88 tai.draw(in: stringRect)
89 stringRect.size.height *= 0.5
90 hi.draw(in: stringRect)
93 private func drawSmallTaihi(in bounds: NSRect) {
97 let x = Int((bounds.width - CGFloat(width)) * 0.5)
98 let y = Int((bounds.height - CGFloat(height)) * 0.5)
99 let rect = NSRect(x: x, y: y, width: width, height: height)
100 let path = NSBezierPath(rect: rect)
101 NSColor.white.setFill()
102 NSColor.black.setStroke()
106 let borderRect = rect.insetBy(dx: 3, dy: 3)
107 let borderPath = NSBezierPath(rect: borderRect)
108 borderPath.lineWidth = 2.0
111 let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(height - 14))
112 let attributes = [ NSAttributedStringKey.foregroundColor: NSColor.lightGray,
113 NSAttributedStringKey.font: fontSize ]
114 let tai = NSAttributedString(string: taiString, attributes: attributes)
115 let hi = NSAttributedString(string: hiString, attributes: attributes)
117 var stringRect = borderRect.insetBy(dx: 2, dy: 2)
118 stringRect.origin.y += 5
119 stringRect.origin.x += 5
120 stringRect.size.height -= 2
121 tai.draw(in: stringRect)
122 stringRect.origin.x += stringRect.width * 0.5 + 1
123 hi.draw(in: stringRect)