2 // NSTextFieldExtension.swift
5 // Created by Hori,Masaki on 2017/09/16.
6 // Copyright © 2017年 Hori,Masaki. All rights reserved.
11 extension NSTextField {
13 func maskImage(middle1: CGFloat, middle2: CGFloat) -> CGImage {
15 let colorspace = CGColorSpaceCreateDeviceGray()
17 guard let maskContext = CGContext(data: nil,
18 width: Int(bounds.width),
19 height: Int(bounds.height),
21 bytesPerRow: Int(bounds.width),
24 else { fatalError("Can not create bitmap context") }
26 let maskGraphicsContext = NSGraphicsContext(cgContext: maskContext, flipped: false)
29 NSGraphicsContext.saveGraphicsState()
30 defer { NSGraphicsContext.restoreGraphicsState() }
32 NSGraphicsContext.current = maskGraphicsContext
34 let gradient = NSGradient(colorsAndLocations: (.white, 0.0),
38 gradient?.draw(in: bounds, angle: 0.0)
40 guard let r = maskContext.makeImage()
41 else { fatalError(" can not create image from context") }
46 func isCharacterProtrude() -> Bool {
48 guard let currentFont = font else {
50 Swift.print("TextField dose not set font")
55 let string = stringValue as NSString
56 let size = string.size(withAttributes: [.font: currentFont])
58 return bounds.size.width - size.width < 3