123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- //
- // KMImageTitleButton.swift
- // PDF Reader Pro
- //
- // Created by lizhe on 2023/1/18.
- //
- import Cocoa
- typealias KMImageTitleButtonAction = (_ view: KMImageTitleButton, _ button: NSButton) -> ()
- @objcMembers class KMImageTitleButton: BaseXibView {
- @IBOutlet weak var iconImage: NSImageView!
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var contentButton: NSButton!
-
- var action: KMImageTitleButtonAction?
- var title: String = "" {
- didSet {
- self.reloadData()
- }
- }
- var imageName: String = "" {
- didSet {
- selectImageName = imageName
- self.reloadData()
- }
- }
-
- var selectImageName: String = "" {
- didSet {
- self.reloadData()
- }
- }
-
- var isEnabled: Bool = true {
- didSet {
- self.iconImage.isEnabled = isEnabled
- self.iconImage.alphaValue = isEnabled ? 1 : 0.5
-
- self.titleLabel.isEnabled = isEnabled
- self.titleLabel.alphaValue = isEnabled ? 1 : 0.5
-
- self.contentButton.isEnabled = isEnabled
- self.contentButton.alphaValue = isEnabled ? 1 : 0.5
- }
- }
- var font: NSFont = NSFont.SFProTextRegularFont(14) {
- didSet {
- self.reloadData()
- }
- }
-
- var textColor: NSColor = NSColor.km_init(hex: "#252629") {
- didSet {
- self.reloadData()
- }
- }
-
- var state: NSControl.StateValue = .off {
- didSet {
- self.contentButton.state = state
- self.reloadData()
- }
- }
-
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- func setup() {
-
- }
-
- func reloadData() {
-
- self.titleLabel.stringValue = NSLocalizedString(self.title, comment: "")
-
- self.titleLabel.wantsLayer = true
- self.titleLabel.font = self.font
- self.titleLabel.textColor = self.textColor
- self.titleLabel.lineBreakMode = .byWordWrapping
- let paragraphStyle = NSMutableParagraphStyle()
- paragraphStyle.lineHeightMultiple = 1.32
- self.titleLabel.attributedStringValue = NSMutableAttributedString(string: title, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, .foregroundColor : self.textColor])
-
- if self.state == .on {
- self.iconImage.image = NSImage(named: self.selectImageName)
- } else {
- self.iconImage.image = NSImage(named: self.imageName)
- }
- }
-
-
- @IBAction func contentButtonAction(_ sender: Any) {
- self.reloadData()
- if action != nil {
- self.action!(self, self.contentButton)
- }
- }
- }
|