StringCompareCellView.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // ImageCompareCellView.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/21.
  6. //
  7. import Foundation
  8. import AppKit
  9. class StringCompareCellView : NSTableCellView {
  10. @IBOutlet var _titleLbl : NSTextField!
  11. @IBOutlet var _degreeInfoLbl : NSTextField!
  12. @IBOutlet var _contentView : NSView!
  13. @IBOutlet var _sepLine : NSView!
  14. @IBOutlet var m_textScrollView: NSScrollView!
  15. @IBOutlet var m_textView: NSTextView!
  16. var _title : String!
  17. var _fileInfo : NSMutableDictionary!
  18. class func shared() -> StringCompareCellView? {
  19. var objects : NSArray!
  20. Bundle.main.loadNibNamed("StringCompareCellView", owner: nil, topLevelObjects: &objects)
  21. for tView in objects {
  22. if let tv = tView as? StringCompareCellView {
  23. return tv
  24. }
  25. }
  26. return nil
  27. }
  28. override func awakeFromNib() {
  29. _sepLine.wantsLayer = true;
  30. _sepLine.layer?.backgroundColor = NSColor.lightGray.cgColor
  31. m_textScrollView.hasVerticalScroller = false
  32. m_textScrollView.horizontalScrollElasticity = .none
  33. }
  34. // Setter & Getter
  35. public func setTitle(_ title:String) {
  36. _title = title;
  37. _titleLbl.stringValue = _title ?? ""
  38. }
  39. public func title() -> String? {
  40. return _title
  41. }
  42. public func setFileInfo(_ fileInfo:NSMutableDictionary) {
  43. _fileInfo = fileInfo;
  44. if nil != _fileInfo {
  45. autoreleasepool {
  46. let resultPath = NSString(string: _fileInfo.resultPath()).appendingPathExtension("rtf")!
  47. let checkData = NSData.init(contentsOfFile: resultPath as String)! as Data
  48. var documentAttributes:NSDictionary!
  49. let attString = NSAttributedString.init(rtf: checkData, documentAttributes: &documentAttributes)
  50. self.m_textView.textStorage?.setAttributedString(attString!)
  51. if (nil == _fileInfo.refFilePath()) {
  52. setTitle(String("[\(_fileInfo.objc().fileType()):\(_fileInfo.objc().name())]\(_fileInfo.fileName())"))
  53. }else {
  54. let lastPathComponent = NSString(string: _fileInfo.refFilePath()!).lastPathComponent
  55. setTitle(String("[\(_fileInfo.objc().fileType()):\(_fileInfo.objc().name())]\(_fileInfo.fileName())/\(lastPathComponent)"))
  56. }
  57. let degree = _fileInfo.degree()
  58. if degree == -1 {
  59. _degreeInfoLbl.stringValue = "对照文件不存在"
  60. _degreeInfoLbl.textColor = NSColor.red
  61. } else {
  62. if abs(degree - 100) > 0 {
  63. _degreeInfoLbl.textColor = NSColor.red
  64. }else {
  65. _degreeInfoLbl.textColor = NSColor.blue
  66. }
  67. _degreeInfoLbl.stringValue = NSString(format: "相似度:%.2f%%", degree) as String
  68. }
  69. }
  70. }else {
  71. setTitle("")
  72. _degreeInfoLbl.stringValue = ""
  73. }
  74. }
  75. public func fileInfo() -> NSMutableDictionary {
  76. return _fileInfo
  77. }
  78. }
  79. extension StringCompareCellView {
  80. class func heightFor(_ fileInfo:NSMutableDictionary) -> CGFloat {
  81. let resultPath = NSString(string: fileInfo.resultPath()).appendingPathExtension("rtf")!
  82. let checkData = NSData.init(contentsOfFile: resultPath as String)! as Data
  83. var documentAttributes:NSDictionary!
  84. let attString = NSAttributedString.init(rtf: checkData, documentAttributes: &documentAttributes)
  85. var retSize = attString!.boundingRect(with: CGSizeMake(990, CGFLOAT_MAX),
  86. options: .usesLineFragmentOrigin.union(.usesFontLeading)).size
  87. return retSize.height + 50
  88. }
  89. }
  90. class CustomScrollView: NSScrollView {
  91. override func scroll(_ point: NSPoint) {
  92. }
  93. override func scrollWheel(with event: NSEvent) {
  94. self.nextResponder?.scrollWheel(with: event);
  95. }
  96. }