Advertisement
Don_Mag

UITextView anim weirdness

Oct 13th, 2023
1,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.88 KB | None | 0 0
  1. class MyTextView: UITextView {
  2.    
  3.     override init(frame: CGRect, textContainer: NSTextContainer?) {
  4.         super.init(frame: frame, textContainer: textContainer)
  5.         commonInit()
  6.     }
  7.     required init?(coder: NSCoder) {
  8.         super.init(coder: coder)
  9.         commonInit()
  10.     }
  11.     private func commonInit() {
  12.         layoutManager.delegate = self
  13.     }
  14.  
  15.     override var bounds: CGRect {
  16.         didSet {
  17.             print("bounds set:", bounds)
  18.         }
  19.     }
  20.     override func layoutSubviews() {
  21.         print(#function, bounds)
  22.         super.layoutSubviews()
  23.     }
  24. }
  25.  
  26. extension MyTextView: NSLayoutManagerDelegate {
  27.     func layoutManager(_ layoutManager: NSLayoutManager, didCompleteLayoutFor textContainer: NSTextContainer?, atEnd layoutFinishedFlag: Bool) {
  28.         print("In: \(#function)", "TextContainerSize:", textContainer?.size)
  29.     }
  30. }
  31.  
  32. class TextViewLayoutTimingVC: UIViewController {
  33.    
  34.     var wConstraint: NSLayoutConstraint!
  35.    
  36.     override func viewDidLoad() {
  37.         super.viewDidLoad()
  38.        
  39.         let textView = MyTextView()
  40.         textView.translatesAutoresizingMaskIntoConstraints = false
  41.         view.addSubview(textView)
  42.        
  43.         let g = view.safeAreaLayoutGuide
  44.        
  45.         wConstraint = textView.widthAnchor.constraint(equalToConstant: 300.0)
  46.        
  47.         NSLayoutConstraint.activate([
  48.             textView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  49.             textView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  50.             textView.heightAnchor.constraint(equalToConstant: 200.0),
  51.             wConstraint,
  52.         ])
  53.  
  54.         textView.backgroundColor = .yellow
  55.         textView.layer.borderColor = UIColor.red.cgColor
  56.         textView.layer.borderWidth = 1
  57.        
  58.         textView.text = "    Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.\n    Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."
  59.  
  60.     }
  61.    
  62.     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  63.         print("\nTouch")
  64.         wConstraint.constant = wConstraint.constant == 300.0 ? 600.0 : 300.0
  65.         UIView.animate(withDuration: 1.0, animations: {
  66.             self.view.layoutIfNeeded()
  67.         })
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement