KMPDFSignatureTextView.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import Cocoa
  2. private let kKMSignayureTextMaxFontSize: CGFloat = 28.0
  3. private let kKMSignayureTextMaxWidth: CGFloat = 300
  4. private let kKMSignayureTextEdgeOffset: CGFloat = 5
  5. @objcMembers class KMVerticallyCenteredSecureTextFieldCell: NSTextFieldCell {
  6. func adjustedFrame(toVerticallyCenterText rect: NSRect) -> NSRect {
  7. let offset = floor((rect.size.height / 2 - (font!.ascender)))
  8. return rect.insetBy(dx: 0.0, dy: offset)
  9. }
  10. override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate delegateObj: Any?, event: NSEvent?) {
  11. super.edit(withFrame: adjustedFrame(toVerticallyCenterText: rect), in: controlView, editor: textObj, delegate: delegateObj, event: event)
  12. }
  13. override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate delegateObj: Any?, start selStart: Int, length selLength: Int) {
  14. super.select(withFrame: adjustedFrame(toVerticallyCenterText: rect), in: controlView, editor: textObj, delegate: delegateObj, start: selStart, length: selLength)
  15. }
  16. override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
  17. super.drawInterior(withFrame: adjustedFrame(toVerticallyCenterText: cellFrame), in: controlView)
  18. }
  19. }
  20. @objcMembers class KMSignayureTextField: NSTextField {
  21. var originalTextColor: NSColor?
  22. override func addSubview(_ view: NSView) {
  23. super.addSubview(view)
  24. if let view1 = view.subviews.first as? NSTextView {
  25. let textview = view1
  26. textview.insertionPointColor = NSColor(red: 102.0 / 255.0, green: 102.0 / 255.0, blue: 102.0 / 255.0, alpha: 1.0)
  27. }
  28. }
  29. }
  30. @objc protocol KMChangeSignatureTextDelegate: NSObjectProtocol {
  31. func changeSignatureText(_ isTrue: Bool)
  32. }
  33. @objcMembers class KMPDFSignatureTextView: NSView, NSTextFieldDelegate {
  34. @IBOutlet var nameTextFiled: KMSignayureTextField!
  35. @IBOutlet var labelWidthLayoutConstraint: NSLayoutConstraint!
  36. @IBOutlet var verticalLineBox: NSBox!
  37. var keyboardColor: NSColor = NSColor(red: 0, green: 0, blue: 0, alpha: 1) {
  38. didSet {
  39. self.nameTextFiled.textColor = keyboardColor
  40. }
  41. }
  42. var fontName: String = "" {
  43. didSet {
  44. var fontSize: CGFloat = 14.0
  45. if !self.nameTextFiled.stringValue.isEmpty {
  46. fontSize = kKMSignayureTextMaxFontSize
  47. }
  48. if let maxFont = NSFont(name: fontName, size: fontSize) {
  49. let size = sizeOfString(self.nameTextFiled.stringValue, withFontSize: maxFont)
  50. var width = size.width + kKMSignayureTextEdgeOffset * 2
  51. var scale: CGFloat = 1.0
  52. scale = min(kKMSignayureTextMaxWidth / width, 60.0 / (size.height + 2))
  53. if width >= 100 {
  54. self.labelWidthLayoutConstraint.constant = width
  55. }
  56. if let font = NSFont(name: fontName, size: fontSize) {
  57. self.nameTextFiled.font = font
  58. }
  59. let isTrue = !self.nameTextFiled.stringValue.isEmpty
  60. if let callback = self.changeSignatureTextCallback {
  61. callback(isTrue)
  62. }
  63. if let delegate = self.delegate {
  64. delegate.changeSignatureText(isTrue)
  65. }
  66. }
  67. }
  68. }
  69. var changeSignatureTextCallback: ((Bool) -> Void)?
  70. weak var delegate: KMChangeSignatureTextDelegate?
  71. override func awakeFromNib() {
  72. super.awakeFromNib()
  73. self.nameTextFiled.placeholderString = NSLocalizedString("Sign Here", comment: "")
  74. self.nameTextFiled.font = NSFont.systemFont(ofSize: 14.0)
  75. // self.verticalLineBox.fillColor = NSColor.black
  76. }
  77. override func draw(_ dirtyRect: NSRect) {
  78. super.draw(dirtyRect)
  79. // Custom drawing code here
  80. }
  81. func clearImage() {
  82. self.nameTextFiled.stringValue = ""
  83. self.nameTextFiled.font = NSFont.systemFont(ofSize: 14.0)
  84. if let callback = self.changeSignatureTextCallback {
  85. callback(false)
  86. }
  87. }
  88. func signatureImage() -> NSImage? {
  89. var rect = CGRect.zero
  90. let string = self.nameTextFiled.stringValue.isEmpty ? "" : self.nameTextFiled.stringValue
  91. let size = sizeOfString(string, withFontSize: self.nameTextFiled.font!)
  92. if size.width == 0 {
  93. return nil
  94. } else {
  95. rect = CGRect(x: kKMSignayureTextEdgeOffset, y: kKMSignayureTextEdgeOffset, width: size.width, height: size.height)
  96. }
  97. let image = NSImage(size: CGSize(width: size.width + kKMSignayureTextEdgeOffset * 2, height: size.height + kKMSignayureTextEdgeOffset * 2))
  98. image.lockFocus()
  99. var dictionary = [NSAttributedString.Key: Any]()
  100. dictionary[NSAttributedString.Key.font] = self.nameTextFiled.font
  101. dictionary[NSAttributedString.Key.foregroundColor] = self.keyboardColor
  102. string.draw(in: rect, withAttributes: dictionary)
  103. image.unlockFocus()
  104. return image
  105. }
  106. func sizeOfString(_ string: String, withFontSize font: NSFont) -> CGSize {
  107. let dictionary: [NSAttributedString.Key: Any] = [
  108. NSAttributedString.Key.font: font,
  109. NSAttributedString.Key.paragraphStyle: NSParagraphStyle.default
  110. ]
  111. let size = string.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: dictionary).size
  112. return size
  113. }
  114. func controlTextDidChange(_ obj: Notification) {
  115. if let textField = obj.object as? NSTextField, textField == self.nameTextFiled {
  116. let string = textField.stringValue.isEmpty ? nil : textField.stringValue
  117. var fontSize: CGFloat = 14.0
  118. if string != nil {
  119. fontSize = kKMSignayureTextMaxFontSize
  120. }
  121. if let maxFont = NSFont(name: self.fontName, size: fontSize) {
  122. let size = sizeOfString(string ?? "", withFontSize: maxFont)
  123. let width = size.width + kKMSignayureTextEdgeOffset * 2
  124. var scale: CGFloat = 1.0
  125. scale = min((self.bounds.size.width - 30) / width, 60.0 / (size.height + 2))
  126. if width >= 100 {
  127. self.labelWidthLayoutConstraint.constant = width
  128. }
  129. if let font = NSFont(name: self.fontName, size: fontSize) {
  130. self.nameTextFiled.font = font
  131. }
  132. let isTrue = !textField.stringValue.isEmpty
  133. if let callback = self.changeSignatureTextCallback {
  134. callback(isTrue)
  135. }
  136. if let delegate = self.delegate {
  137. delegate.changeSignatureText(isTrue)
  138. }
  139. }
  140. }
  141. }
  142. }