@@ -54,6 +54,17 @@ public class TextLayoutManager: NSObject {
5454 }
5555 }
5656
57+ /// The amount of extra vertical padding used to lay out lines in before they come into view.
58+ ///
59+ /// This solves a small problem with layout performance, if you're seeing layout lagging behind while scrolling,
60+ /// adjusting this value higher may help fix that.
61+ /// Defaults to `350`.
62+ public var verticalLayoutPadding : CGFloat = 350 {
63+ didSet {
64+ setNeedsLayout ( )
65+ }
66+ }
67+
5768 // MARK: - Internal
5869
5970 weak var textStorage : NSTextStorage ?
@@ -215,10 +226,12 @@ public class TextLayoutManager: NSObject {
215226 }
216227
217228 /// Ends a transaction. When called, the layout manager will layout any necessary lines.
218- public func endTransaction( ) {
229+ public func endTransaction( forceLayout : Bool = false ) {
219230 transactionCounter -= 1
220231 if transactionCounter == 0 {
221- setNeedsLayout ( )
232+ if forceLayout {
233+ setNeedsLayout ( )
234+ }
222235 layoutLines ( )
223236 } else if transactionCounter < 0 {
224237 // swiftlint:disable:next line_length
@@ -237,8 +250,8 @@ public class TextLayoutManager: NSObject {
237250 return
238251 }
239252 CATransaction . begin ( )
240- let minY = max ( visibleRect. minY, 0 )
241- let maxY = max ( visibleRect. maxY, 0 )
253+ let minY = max ( visibleRect. minY - verticalLayoutPadding , 0 )
254+ let maxY = max ( visibleRect. maxY + verticalLayoutPadding , 0 )
242255 let originalHeight = lineStorage. height
243256 var usedFragmentIDs = Set < UUID > ( )
244257 var forceLayout : Bool = needsLayout
@@ -283,6 +296,8 @@ public class TextLayoutManager: NSObject {
283296 newVisibleLines. insert ( linePosition. data. id)
284297 }
285298
299+ CATransaction . commit ( )
300+
286301 // Enqueue any lines not used in this layout pass.
287302 viewReuseQueue. enqueueViews ( notInSet: usedFragmentIDs)
288303
@@ -302,7 +317,6 @@ public class TextLayoutManager: NSObject {
302317 }
303318
304319 needsLayout = false
305- CATransaction . commit ( )
306320 }
307321
308322 /// Lays out a single text line.
0 commit comments