CPDFTipsView.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // CPDFTipsView.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. public class CPDFTipsView: UIView {
  14. private var searchLabel = UILabel()
  15. public override init(frame: CGRect) {
  16. super.init(frame: frame)
  17. backgroundColor = UIColor.init(red: 28.0/255.0, green: 28.0/255.0, blue: 30.0/255.0, alpha: 1.9)
  18. searchLabel.textColor = UIColor.white
  19. addSubview(searchLabel)
  20. }
  21. required init?(coder: NSCoder) {
  22. fatalError("init(coder:) has not been implemented")
  23. }
  24. public func postAlertWithMessage(message:String) {
  25. searchLabel.text = message
  26. searchLabel.sizeToFit()
  27. searchLabel.font = UIFont.systemFont(ofSize: 14.0)
  28. let paragraphStyle = NSMutableParagraphStyle()
  29. paragraphStyle.lineBreakMode = .byWordWrapping
  30. paragraphStyle.alignment = .left
  31. paragraphStyle.lineSpacing = 2
  32. let attributes: [NSAttributedString.Key: Any] = [
  33. .font: UIFont.systemFont(ofSize: 14),
  34. .paragraphStyle: paragraphStyle,
  35. .foregroundColor: UIColor.black
  36. ]
  37. let limitSize = CGSize(width: 250, height: 10000)
  38. let rect = (message as NSString).boundingRect(
  39. with: limitSize,
  40. options: [.usesLineFragmentOrigin, .usesFontLeading],
  41. attributes: attributes,
  42. context: nil
  43. )
  44. searchLabel.frame = CGRect(x: 0, y: 0, width: 250, height: rect.height)
  45. }
  46. public func showView(_ subView: UIView) {
  47. self.frame = CGRect(x: CGFloat(Int((subView.frame.size.width - 250.0))/2) - 10, y: CGFloat(Int((subView.frame.size.height - searchLabel.height))/2) - 10, width: searchLabel.width + 20, height: searchLabel.height + 20)
  48. self.layer.cornerRadius = 5
  49. subView.addSubview(self)
  50. searchLabel.frame = CGRect(x: 10, y: 10, width: searchLabel.width, height: searchLabel.height)
  51. self.perform(#selector(hidePageView), with: nil, afterDelay: 1.0)
  52. }
  53. @objc func showTip() {
  54. if self.isHidden {
  55. self.alpha = 1.0
  56. self.isHidden = false
  57. }
  58. self.perform(#selector(hidePageView), with: nil, afterDelay: 1.0)
  59. }
  60. @objc func hidePageView() {
  61. if self.isHidden {
  62. return
  63. }
  64. UIView.animate(withDuration: 0.3, animations: {
  65. self.alpha = 0.0
  66. }) { (finished) in
  67. self.isHidden = true
  68. }
  69. }
  70. }