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) else {
18 fatalError("Can not load Taihi.txt")
21 guard (taihiString as NSString).length == 2 else {
23 fatalError("Taihi string is not 2 charactor")
29 var controlSize: NSControl.ControlSize = .regular
30 private var taiString: String {
32 let s = GuardEscapedView.taihiStrings
33 let range = s.index(s.startIndex, offsetBy: 0)
35 return String(s[range])
38 private var hiString: String {
40 let s = GuardEscapedView.taihiStrings
41 let range = s.index(s.endIndex, offsetBy: -1)
43 return String(s[range])
46 override func draw(_ dirtyRect: NSRect) {
50 let bounds = self.bounds
51 NSColor(calibratedWhite: 0.9, alpha: 0.8).setFill()
52 NSBezierPath(rect: bounds).fill()
56 case .regular: drawTaihi(in: bounds)
58 case .small, .mini: drawSmallTaihi(in: bounds)
63 private func drawTaihi(in bounds: NSRect) {
65 let rotate = NSAffineTransform()
66 rotate.translateX(by: 0.0, yBy: 65.0)
67 rotate.rotate(byDegrees: -27)
72 let x = Int((bounds.width - CGFloat(width)) * 0.5)
73 let y = Int((bounds.height - CGFloat(height)) * 0.5)
74 let rect = NSRect(x: x, y: y, width: width, height: height)
75 let path = NSBezierPath(rect: rect)
76 NSColor.white.setFill()
77 NSColor.black.setStroke()
81 let borderRect = rect.insetBy(dx: 3, dy: 3)
82 let borderPath = NSBezierPath(rect: borderRect)
83 borderPath.lineWidth = 2.0
86 let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(width - 10))
87 let attributes = [ NSAttributedStringKey.foregroundColor: NSColor.lightGray,
88 NSAttributedStringKey.font: fontSize ]
89 let tai = NSAttributedString(string: taiString, attributes: attributes)
90 let hi = NSAttributedString(string: hiString, attributes: attributes)
92 var stringRect = borderRect.insetBy(dx: 2, dy: 2)
93 stringRect.origin.y += 4
94 stringRect.origin.x += 1.5
95 stringRect.size.height -= 2
96 tai.draw(in: stringRect)
97 stringRect.size.height *= 0.5
98 hi.draw(in: stringRect)
101 private func drawSmallTaihi(in bounds: NSRect) {
105 let x = Int((bounds.width - CGFloat(width)) * 0.5)
106 let y = Int((bounds.height - CGFloat(height)) * 0.5)
107 let rect = NSRect(x: x, y: y, width: width, height: height)
108 let path = NSBezierPath(rect: rect)
109 NSColor.white.setFill()
110 NSColor.black.setStroke()
114 let borderRect = rect.insetBy(dx: 3, dy: 3)
115 let borderPath = NSBezierPath(rect: borderRect)
116 borderPath.lineWidth = 2.0
119 let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(height - 14))
120 let attributes = [ NSAttributedStringKey.foregroundColor: NSColor.lightGray,
121 NSAttributedStringKey.font: fontSize ]
122 let tai = NSAttributedString(string: taiString, attributes: attributes)
123 let hi = NSAttributedString(string: hiString, attributes: attributes)
125 var stringRect = borderRect.insetBy(dx: 2, dy: 2)
126 stringRect.origin.y += 5
127 stringRect.origin.x += 5
128 stringRect.size.height -= 2
129 tai.draw(in: stringRect)
130 stringRect.origin.x += stringRect.width * 0.5 + 1
131 hi.draw(in: stringRect)