OSDN Git Service

関数名を変更
[kcd/KCD.git] / KCD / NSTextFieldExtension.swift
1 //
2 //  NSTextFieldExtension.swift
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2017/09/16.
6 //  Copyright © 2017年 Hori,Masaki. All rights reserved.
7 //
8
9 import Cocoa
10
11 extension NSTextField {
12     
13     func maskImage(middle1: CGFloat, middle2: CGFloat) -> CGImage {
14         
15         guard let maskContext = CGContext(data: nil,
16                                           width: Int(bounds.width),
17                                           height: Int(bounds.height),
18                                           bitsPerComponent: 8,
19                                           bytesPerRow: Int(bounds.width),
20                                           space: CGColorSpaceCreateDeviceGray(),
21                                           bitmapInfo: 0) else {
22                                             
23                                             fatalError("Can not create bitmap context")
24         }
25         
26         let maskGraphicsContext = NSGraphicsContext(cgContext: maskContext, flipped: false)
27         
28         NSGraphicsContext.saveGraphicsState()
29         defer { NSGraphicsContext.restoreGraphicsState() }
30         
31         NSGraphicsContext.current = maskGraphicsContext
32         
33         let gradient = NSGradient(colorsAndLocations: (.white, 0.0),
34                                   (.white, middle1),
35                                   (.black, middle2),
36                                   (.black, 1.0))
37         gradient?.draw(in: bounds, angle: 0.0)
38         
39         guard let r = maskContext.makeImage() else { fatalError(" can not create image from context") }
40         
41         return r
42     }
43     
44     func isCharacterProtrude() -> Bool {
45         
46         guard let currentFont = font else { return Logger.shared.log("TextField dose not set font", value: false) }
47         
48         let string = stringValue as NSString
49         let size = string.size(withAttributes: [.font: currentFont])
50         
51         return bounds.size.width - size.width < 3
52     }
53     
54 }