UITextView using TextKit 1
I received this warning and was surprised to know that there are 2 TextKits, TextKit 1 and 2 in the iOS SDK.
UITextView 0x1063c9000 is switching to TextKit 1 compatibility mode because its layoutManager was accessed. Break on void _UITextViewEnablingCompatibilityMode(UITextView *__strong, BOOL) to debug.
I use UITextView
’s LayoutManager
so I can detect the text or a group of texts that the user has tapped or selected.
func textViewDidChangeSelection(_ textView: UITextView) {
textView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(textTapped(_:))))
}
@objc private func textTapped(_ sender: UITapGestureRecognizer) {
let textView = sender.view as! CustomTextView
let layoutManger = textView.layoutManager
// Location of the tap
var location = sender.location(in: textView)
location.x -= textView.textContainerInset.left
location.y -= textView.textContainerInset.top
// Character index at tap location
let characterIndex = layoutManger.characterIndex(for: location, in: textView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
}
So based on the answer on the question Replacing layout manager in UITextView using TextKit 1, I need to override NSLayoutManager.
class CustomTextView: UITextView {
private var customLayoutManager = NSLayoutManager()
init(someData: String) {
self.someData = someData
super.init(frame: .zero, textContainer: nil)
customLayoutManager.textStorage = self.textStorage
customLayoutManager.addTextContainer(self.textContainer)
self.textContainer.replaceLayoutManager(customLayoutManager)
}
override var layoutManager: NSLayoutManager {
return customLayoutManager
}
}