KMImageTitleButton.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // KMImageTitleButton.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/1/18.
  6. //
  7. import Cocoa
  8. typealias KMImageTitleButtonAction = (_ view: KMImageTitleButton, _ button: NSButton) -> ()
  9. @objcMembers class KMImageTitleButton: KMBaseXibView {
  10. @IBOutlet weak var iconImage: NSImageView!
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var contentButton: NSButton!
  13. var action: KMImageTitleButtonAction?
  14. var title: String = "" {
  15. didSet {
  16. self.reloadData()
  17. }
  18. }
  19. var imageName: String = "" {
  20. didSet {
  21. selectImageName = imageName
  22. self.reloadData()
  23. }
  24. }
  25. var selectImageName: String = "" {
  26. didSet {
  27. self.reloadData()
  28. }
  29. }
  30. var isEnabled: Bool = true {
  31. didSet {
  32. self.iconImage.isEnabled = isEnabled
  33. self.iconImage.alphaValue = isEnabled ? 1 : 0.5
  34. self.titleLabel.isEnabled = isEnabled
  35. self.titleLabel.alphaValue = isEnabled ? 1 : 0.5
  36. self.contentButton.isEnabled = isEnabled
  37. self.contentButton.alphaValue = isEnabled ? 1 : 0.5
  38. }
  39. }
  40. var font: NSFont = NSFont.SFProTextRegularFont(14) {
  41. didSet {
  42. self.reloadData()
  43. }
  44. }
  45. var textColor: NSColor = NSColor.km_init(hex: "#252629") {
  46. didSet {
  47. self.reloadData()
  48. }
  49. }
  50. var state: NSControl.StateValue = .off {
  51. didSet {
  52. self.contentButton.state = state
  53. self.reloadData()
  54. }
  55. }
  56. override func draw(_ dirtyRect: NSRect) {
  57. super.draw(dirtyRect)
  58. // Drawing code here.
  59. }
  60. override func setup() {
  61. super.setup()
  62. // self.titleLabel.font = NSFont(name: "SFProText-Regular", size: 14)
  63. }
  64. override func reloadData() {
  65. super.reloadData()
  66. self.titleLabel.stringValue = NSLocalizedString(self.title, comment: "")
  67. self.titleLabel.wantsLayer = true
  68. self.titleLabel.font = self.font
  69. self.titleLabel.textColor = self.textColor
  70. self.titleLabel.lineBreakMode = .byWordWrapping
  71. let paragraphStyle = NSMutableParagraphStyle()
  72. paragraphStyle.lineHeightMultiple = 1.32
  73. self.titleLabel.attributedStringValue = NSMutableAttributedString(string: title, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, .foregroundColor : self.textColor])
  74. if self.state == .on {
  75. self.iconImage.image = NSImage(named: self.selectImageName)
  76. } else {
  77. self.iconImage.image = NSImage(named: self.imageName)
  78. }
  79. }
  80. @IBAction func contentButtonAction(_ sender: Any) {
  81. self.reloadData()
  82. if action != nil {
  83. self.action!(self, self.contentButton)
  84. }
  85. }
  86. }