@@ -640,16 +640,6 @@ import STTextViewCommon
640
640
}
641
641
642
642
open override func sizeToFit( ) {
643
- let gutterWidth = gutterView? . frame. width ?? 0
644
- contentView. frame. origin. x = gutterWidth
645
- contentView. frame. size. width = max ( textLayoutManager. usageBoundsForTextContainer. size. width + textContainer. lineFragmentPadding, frame. width - gutterWidth)
646
- contentView. frame. size. height = max ( textLayoutManager. usageBoundsForTextContainer. size. height, frame. height)
647
- contentSize = contentView. frame. size
648
-
649
- super. sizeToFit ( )
650
-
651
- _configureTextContainerSize ( )
652
-
653
643
// Estimate `usageBoundsForTextContainer` size is based on performed layout.
654
644
// If layout didn't happen for the whole document, it only cover
655
645
// the fragment that is known. And even after ensureLayout for the whole document
@@ -668,7 +658,41 @@ import STTextViewCommon
668
658
// Asking for the end location result in estimated `usageBoundsForTextContainer`
669
659
// that eventually get right as more and more layout happen (when scrolling)
670
660
661
+ // Estimated text container size to layout document
671
662
textLayoutManager. ensureLayout ( for: NSTextRange ( location: textLayoutManager. documentRange. endLocation) )
663
+
664
+ super. sizeToFit ( )
665
+
666
+ let usageBoundsForTextContainer = textLayoutManager. usageBoundsForTextContainer
667
+ logger. debug ( " usageBoundsForTextContainer \( usageBoundsForTextContainer. debugDescription) \( #function) " )
668
+
669
+ let gutterWidth = gutterView? . frame. width ?? 0
670
+ let verticalScrollInset = contentInset. top + contentInset. bottom
671
+ let visibleRectSize = self . bounds. size
672
+
673
+ let frameSize : CGSize
674
+ if isHorizontallyResizable {
675
+ // no-wrapping
676
+ frameSize = CGSize (
677
+ width: max ( usageBoundsForTextContainer. size. width + gutterWidth + textContainer. lineFragmentPadding, visibleRectSize. width) ,
678
+ height: max ( usageBoundsForTextContainer. size. height, visibleRectSize. height - verticalScrollInset)
679
+ )
680
+ } else {
681
+ // wrapping
682
+ frameSize = CGSize (
683
+ width: visibleRectSize. width - gutterWidth,
684
+ height: max ( usageBoundsForTextContainer. size. height, visibleRectSize. height - verticalScrollInset)
685
+ )
686
+ }
687
+
688
+ if !frame. size. isAlmostEqual ( to: frameSize) {
689
+ logger. debug ( " contentView.frame.size ( \( frameSize. width) , \( frameSize. height) ) \( #function) " )
690
+ self . contentView. frame. origin. x = gutterWidth
691
+ self . contentView. frame. size = frameSize
692
+ self . contentSize = frameSize
693
+ }
694
+
695
+ _configureTextContainerSize ( )
672
696
}
673
697
674
698
// Update textContainer width to match textview width if track textview width
0 commit comments