KMTableHeaderCell.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // KMTableHeaderCell.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by kdanmobile on 2023/10/26.
  6. //
  7. import Cocoa
  8. class KMTableHeaderCell: NSTableHeaderCell{
  9. override init(textCell string: String) {
  10. super.init(textCell: string)
  11. self.drawsBackground = false
  12. }
  13. required init(coder: NSCoder) {
  14. super.init(coder: coder)
  15. }
  16. override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
  17. super.draw(withFrame: cellFrame, in: controlView)
  18. cellFrame.fill()
  19. KMAppearance.Layout.l_2Color().set()
  20. self.drawInterior(withFrame: cellFrame, inView: controlView)
  21. }
  22. func drawInterior(withFrame cellFrame: NSRect, inView controlView: NSView) {
  23. var titleRect = self.titleRect(forBounds: cellFrame)
  24. titleRect.fill()
  25. KMAppearance.Layout.l_2Color().set()
  26. let layoutManager = NSLayoutManager()
  27. let lineHeight = layoutManager.defaultLineHeight(for: self.font!)
  28. titleRect.origin.y = (titleRect.size.height - lineHeight) / 2.0
  29. titleRect.size.height = lineHeight
  30. if self.alignment == NSTextAlignment.left {
  31. self.attributedStringValue.draw(with: titleRect, options: .usesLineFragmentOrigin)
  32. } else if self.alignment == NSTextAlignment.center {
  33. let width = self.widthOfString(self.title, withFont: self.font!)
  34. titleRect.origin.x += (titleRect.size.width - width) / 2.0
  35. titleRect.size.width = width
  36. self.attributedStringValue.draw(with: titleRect, options: .usesLineFragmentOrigin)
  37. } else {
  38. self.attributedStringValue.draw(with: titleRect, options: .usesLineFragmentOrigin)
  39. }
  40. }
  41. func drawSortIndicator(withFrame cellFrame: NSRect, inView controlView: NSView, ascending: Bool, priority: Int) {
  42. }
  43. override func sortIndicatorRect(forBounds rect: NSRect) -> NSRect {
  44. return NSRect.zero
  45. }
  46. func widthOfString(_ string: String, withFont font: NSFont) -> CGFloat {
  47. let attributes = [NSAttributedString.Key.font: font]
  48. return NSAttributedString(string: string, attributes: attributes).size().width
  49. }
  50. }