PhotoEditor+UITextView.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // PhotoEditor+UITextView.swift
  3. // Pods
  4. //
  5. // Created by Mohamed Hamed on 6/16/17.
  6. //
  7. //
  8. import Foundation
  9. import UIKit
  10. extension PhotoEditorViewController: UITextViewDelegate {
  11. public func textViewDidChange(_ textView: UITextView) {
  12. let rotation = atan2(textView.transform.b, textView.transform.a)
  13. if rotation == 0 {
  14. let oldFrame = textView.frame
  15. let sizeToFit = textView.sizeThatFits(CGSize(width: oldFrame.width, height:CGFloat.greatestFiniteMagnitude))
  16. textView.frame.size = CGSize(width: oldFrame.width, height: sizeToFit.height)
  17. }
  18. }
  19. public func textViewDidBeginEditing(_ textView: UITextView) {
  20. isTyping = true
  21. lastTextViewTransform = textView.transform
  22. lastTextViewTransCenter = textView.center
  23. lastTextViewFont = textView.font!
  24. activeTextView = textView
  25. textView.superview?.bringSubviewToFront(textView)
  26. textView.font = UIFont(name: "Helvetica", size: 30)
  27. UIView.animate(withDuration: 0.3,
  28. animations: {
  29. textView.transform = CGAffineTransform.identity
  30. textView.center = CGPoint(x: UIScreen.main.bounds.width / 2,
  31. y: UIScreen.main.bounds.height / 5)
  32. }, completion: nil)
  33. }
  34. public func textViewDidEndEditing(_ textView: UITextView) {
  35. guard lastTextViewTransform != nil && lastTextViewTransCenter != nil && lastTextViewFont != nil
  36. else {
  37. return
  38. }
  39. activeTextView = nil
  40. textView.font = self.lastTextViewFont!
  41. UIView.animate(withDuration: 0.3,
  42. animations: {
  43. textView.transform = self.lastTextViewTransform!
  44. textView.center = self.lastTextViewTransCenter!
  45. }, completion: nil)
  46. }
  47. }