CNavigationBarTitleButton.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // CNavigationBarTitleButton.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. import Foundation
  14. public class CNavigationBarTitleButton: UIButton {
  15. override init(frame: CGRect) {
  16. super.init(frame: frame)
  17. self.titleLabel?.font = UIFont.systemFont(ofSize: 17.0)
  18. self.setTitleColor(.black, for: .normal)
  19. self.titleLabel?.textAlignment = .right
  20. self.titleLabel?.numberOfLines = 0
  21. self.adjustsImageWhenDisabled = false
  22. self.imageView?.contentMode = .center
  23. }
  24. required init?(coder: NSCoder) {
  25. fatalError("init(coder:) has not been implemented")
  26. }
  27. public override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
  28. let height = contentRect.size.height
  29. let width = height
  30. let x = self.frame.size.width - width
  31. let y: CGFloat = 0.0
  32. return CGRect(x: x, y: y, width: width, height: height)
  33. }
  34. public override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
  35. let height = contentRect.size.height
  36. let width = self.frame.size.width - height
  37. let x: CGFloat = 0
  38. let y: CGFloat = 0
  39. return CGRect(x: x, y: y, width: width, height: height)
  40. }
  41. public override func setTitle(_ title: String?, for state: UIControl.State) {
  42. super.setTitle(title, for: state)
  43. let param: [NSAttributedString.Key: Any] = [NSAttributedString.Key.font: self.titleLabel?.font ?? UIFont.systemFont(ofSize: 17.0)]
  44. let titleWidth = title?.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: self.frame.size.height), options: [.usesLineFragmentOrigin], attributes: param, context: nil).size.width ?? 0.0
  45. var frame = self.frame
  46. frame.size.width = titleWidth + self.frame.size.height + 10
  47. self.frame = frame
  48. }
  49. }