Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyTextView: UITextView {
- override init(frame: CGRect, textContainer: NSTextContainer?) {
- super.init(frame: frame, textContainer: textContainer)
- commonInit()
- }
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- commonInit()
- }
- private func commonInit() {
- layoutManager.delegate = self
- }
- override var bounds: CGRect {
- didSet {
- print("bounds set:", bounds)
- }
- }
- override func layoutSubviews() {
- print(#function, bounds)
- super.layoutSubviews()
- }
- }
- extension MyTextView: NSLayoutManagerDelegate {
- func layoutManager(_ layoutManager: NSLayoutManager, didCompleteLayoutFor textContainer: NSTextContainer?, atEnd layoutFinishedFlag: Bool) {
- print("In: \(#function)", "TextContainerSize:", textContainer?.size)
- }
- }
- class TextViewLayoutTimingVC: UIViewController {
- var wConstraint: NSLayoutConstraint!
- override func viewDidLoad() {
- super.viewDidLoad()
- let textView = MyTextView()
- textView.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(textView)
- let g = view.safeAreaLayoutGuide
- wConstraint = textView.widthAnchor.constraint(equalToConstant: 300.0)
- NSLayoutConstraint.activate([
- textView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
- textView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
- textView.heightAnchor.constraint(equalToConstant: 200.0),
- wConstraint,
- ])
- textView.backgroundColor = .yellow
- textView.layer.borderColor = UIColor.red.cgColor
- textView.layer.borderWidth = 1
- 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."
- }
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- print("\nTouch")
- wConstraint.constant = wConstraint.constant == 300.0 ? 600.0 : 300.0
- UIView.animate(withDuration: 1.0, animations: {
- self.view.layoutIfNeeded()
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement