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
20 else { fatalError("Taihi string is not 2 charactor") }
25 var controlSize: NSControlSize = .regular
26 private var taiString: String {
28 let s = GuardEscapedView.taihiStrings
29 let range = s.index(s.startIndex, offsetBy: 0)
31 return String(s[range])
34 private var hiString: String {
36 let s = GuardEscapedView.taihiStrings
37 let range = s.index(s.endIndex, offsetBy: -1)
39 return String(s[range])
42 override func draw(_ dirtyRect: NSRect) {
46 let bounds = self.bounds
47 NSColor(calibratedWhite: 0.9, alpha: 0.8).setFill()
48 NSBezierPath(rect: bounds).fill()
51 case .regular: drawTaihi(in: bounds)
52 case .small, .mini: drawSmallTaihi(in: bounds)
56 private func drawTaihi(in bounds: NSRect) {
58 let rotate = NSAffineTransform()
59 rotate.translateX(by: 0.0, yBy: 65.0)
60 rotate.rotate(byDegrees: -27)
65 let x = Int((bounds.width - CGFloat(width)) * 0.5)
66 let y = Int((bounds.height - CGFloat(height)) * 0.5)
67 let rect = NSRect(x: x, y: y, width: width, height: height)
68 let path = NSBezierPath(rect: rect)
69 NSColor.white.setFill()
70 NSColor.black.setStroke()
74 let borderRect = rect.insetBy(dx: 3, dy: 3)
75 let borderPath = NSBezierPath(rect: borderRect)
76 borderPath.lineWidth = 2.0
79 let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(width - 10))
80 let attributes = [ NSForegroundColorAttributeName: NSColor.lightGray,
81 NSFontAttributeName: fontSize ]
82 let tai = NSAttributedString(string: taiString, attributes: attributes)
83 let hi = NSAttributedString(string: hiString, attributes: attributes)
85 var stringRect = borderRect.insetBy(dx: 2, dy: 2)
86 stringRect.origin.y += 4
87 stringRect.origin.x += 1.5
88 stringRect.size.height -= 2
89 tai.draw(in: stringRect)
90 stringRect.size.height *= 0.5
91 hi.draw(in: stringRect)
94 private func drawSmallTaihi(in bounds: NSRect) {
98 let x = Int((bounds.width - CGFloat(width)) * 0.5)
99 let y = Int((bounds.height - CGFloat(height)) * 0.5)
100 let rect = NSRect(x: x, y: y, width: width, height: height)
101 let path = NSBezierPath(rect: rect)
102 NSColor.white.setFill()
103 NSColor.black.setStroke()
107 let borderRect = rect.insetBy(dx: 3, dy: 3)
108 let borderPath = NSBezierPath(rect: borderRect)
109 borderPath.lineWidth = 2.0
112 let fontSize = NSFont.boldSystemFont(ofSize: CGFloat(height - 14))
113 let attributes = [ NSForegroundColorAttributeName: NSColor.lightGray,
114 NSFontAttributeName: fontSize ]
115 let tai = NSAttributedString(string: taiString, attributes: attributes)
116 let hi = NSAttributedString(string: hiString, attributes: attributes)
118 var stringRect = borderRect.insetBy(dx: 2, dy: 2)
119 stringRect.origin.y += 5
120 stringRect.origin.x += 5
121 stringRect.size.height -= 2
122 tai.draw(in: stringRect)
123 stringRect.origin.x += stringRect.width * 0.5 + 1
124 hi.draw(in: stringRect)