Skip to content

Commit 83aa14f

Browse files
committed
Fix textLayoutManager property and add setup method
The PR adds a proper setup method for the textLayoutManager and ensures connections are properly maintained when setting a new one. It fixes delegate assignment, notification handling, and connection with the textContentManager when the textLayoutManager is changed.
1 parent a74fb80 commit 83aa14f

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

Sources/STTextViewUIKit/STTextView.swift

+23-11
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,14 @@ import STTextViewCommon
3434
open var returnKeyType: UIReturnKeyType = .default
3535

3636
/// 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+
}
3841
didSet {
39-
textContentManager.removeTextLayoutManager(oldValue)
4042
textContentManager.addTextLayoutManager(textLayoutManager)
4143
textContentManager.primaryTextLayoutManager = textLayoutManager
44+
setupTextLayoutManager(textLayoutManager)
4245
}
4346
}
4447

@@ -441,14 +444,13 @@ import STTextViewCommon
441444

442445
super.init(frame: frame)
443446

447+
setupTextLayoutManager(textLayoutManager)
448+
444449
contentView.clipsToBounds = clipsToBounds
445450
lineHighlightView.clipsToBounds = clipsToBounds
446451

447452
setSelectedTextRange(NSTextRange(location: textLayoutManager.documentRange.location), updateLayout: false)
448453

449-
textLayoutManager.delegate = self
450-
textLayoutManager.textViewportLayoutController.delegate = self
451-
452454
addSubview(contentView)
453455
contentView.addSubview(lineHighlightView)
454456

@@ -460,8 +462,23 @@ import STTextViewCommon
460462

461463
updateEditableInteraction()
462464
isGutterVisible = showsLineNumbers
465+
}
466+
467+
@available(*, unavailable)
468+
required public init?(coder: NSCoder) {
469+
fatalError("init(coder:) has not been implemented")
470+
}
463471

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
465482
guard let self = self else { return }
466483
let textViewNotification = Notification(name: Self.didChangeSelectionNotification, object: self, userInfo: notification.userInfo)
467484

@@ -471,11 +488,6 @@ import STTextViewCommon
471488
}
472489
}
473490

474-
@available(*, unavailable)
475-
required public init?(coder: NSCoder) {
476-
fatalError("init(coder:) has not been implemented")
477-
}
478-
479491
/// This action method shows or hides the ruler, if the receiver is enclosed in a scroll view
480492
@objc public func toggleRuler(_ sender: Any?) {
481493
isGutterVisible.toggle()

0 commit comments

Comments
 (0)