@@ -34,11 +34,14 @@ import STTextViewCommon
34
34
open var returnKeyType : UIReturnKeyType = . default
35
35
36
36
/// The manager that lays out text for the text view's text container.
37
- @objc open var textLayoutManager : NSTextLayoutManager {
37
+ @objc dynamic open var textLayoutManager : NSTextLayoutManager {
38
+ willSet {
39
+ textContentManager. removeTextLayoutManager ( newValue)
40
+ }
38
41
didSet {
39
- textContentManager. removeTextLayoutManager ( oldValue)
40
42
textContentManager. addTextLayoutManager ( textLayoutManager)
41
43
textContentManager. primaryTextLayoutManager = textLayoutManager
44
+ setupTextLayoutManager ( textLayoutManager)
42
45
}
43
46
}
44
47
@@ -441,14 +444,13 @@ import STTextViewCommon
441
444
442
445
super. init ( frame: frame)
443
446
447
+ setupTextLayoutManager ( textLayoutManager)
448
+
444
449
contentView. clipsToBounds = clipsToBounds
445
450
lineHighlightView. clipsToBounds = clipsToBounds
446
451
447
452
setSelectedTextRange ( NSTextRange ( location: textLayoutManager. documentRange. location) , updateLayout: false )
448
453
449
- textLayoutManager. delegate = self
450
- textLayoutManager. textViewportLayoutController. delegate = self
451
-
452
454
addSubview ( contentView)
453
455
contentView. addSubview ( lineHighlightView)
454
456
@@ -460,8 +462,23 @@ import STTextViewCommon
460
462
461
463
updateEditableInteraction ( )
462
464
isGutterVisible = showsLineNumbers
465
+ }
466
+
467
+ @available ( * , unavailable)
468
+ required public init ? ( coder: NSCoder ) {
469
+ fatalError ( " init(coder:) has not been implemented " )
470
+ }
463
471
464
- NotificationCenter . default. addObserver ( forName: STTextLayoutManager . didChangeSelectionNotification, object: textLayoutManager, queue: . main) { [ weak self] notification in
472
+ private var didChangeSelectionNotificationObserver : NSObjectProtocol ?
473
+ private func setupTextLayoutManager( _ textLayoutManager: NSTextLayoutManager ) {
474
+ textLayoutManager. delegate = self
475
+ textLayoutManager. textViewportLayoutController. delegate = self
476
+
477
+ // Forward didChangeSelectionNotification from STTextLayoutManager
478
+ if let didChangeSelectionNotificationObserver {
479
+ NotificationCenter . default. removeObserver ( didChangeSelectionNotificationObserver)
480
+ }
481
+ didChangeSelectionNotificationObserver = NotificationCenter . default. addObserver ( forName: STTextLayoutManager . didChangeSelectionNotification, object: textLayoutManager, queue: . main) { [ weak self] notification in
465
482
guard let self = self else { return }
466
483
let textViewNotification = Notification ( name: Self . didChangeSelectionNotification, object: self , userInfo: notification. userInfo)
467
484
@@ -471,11 +488,6 @@ import STTextViewCommon
471
488
}
472
489
}
473
490
474
- @available ( * , unavailable)
475
- required public init ? ( coder: NSCoder ) {
476
- fatalError ( " init(coder:) has not been implemented " )
477
- }
478
-
479
491
/// This action method shows or hides the ruler, if the receiver is enclosed in a scroll view
480
492
@objc public func toggleRuler( _ sender: Any ? ) {
481
493
isGutterVisible. toggle ( )
0 commit comments