我正在开发一个macOS应用程序。我想要编写一个代码来显示NSTextView中单击位置上的光标。所以我在StackOverFlow搜索发现了the following code for iOS
@objc func didTapTextView(recognizer: UITapGestureRecognizer) {
if recognizer.state == .ended {
textView.isEditable = true
textView.becomeFirstResponder()
let location = recognizer.location(in: textView)
if let position = textView.closestPosition(to: location) {
let uiTextRange = textView.textRange(from: position, to: position)
if let start = uiTextRange?.start, let end = uiTextRange?.end {
let loc = textView.offset(from: textView.beginningOfDocument, to: position)
let length = textView.offset(from: start, to: end)
textView.selectedRange = NSMakeRange(loc, length)
}
}
}
}因此,要将代码从iOS转换为macOS,我可以:
NSClickGestureRecognizer
UITapGestureRecognizer更改为textView.becomeFirstResponder()到textView.window?.makeFirstResponder(textView)问题是:
我无法将方法.closestPosition、.textRange和.offset转换为macOS,因为它们只在UIKit中可用,并且它们返回UITextPosition和UITextRange等UIKit值。
最小可重现性示例
要重现这个问题,只需创建一个基于文档的Cocoa应用程序(macOS),并添加一个可滚动的TextView,那么代码如下:
import Cocoa
class ViewController: NSViewController, NSGestureRecognizerDelegate {
@IBOutlet var textView: NSTextView!
override func viewDidLoad() {
super.viewDidLoad()
// Set tap gesture
let singleClickGesture = NSClickGestureRecognizer(target: self, action: #selector(singleClickGesture(_:)))
singleClickGesture.numberOfClicksRequired = 1 // single
singleClickGesture.delegate = self
textView.addGestureRecognizer(singleClickGesture)
// create attributed string
let myAttrString = NSMutableAttributedString(string: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
// Write it to the Text View
textView.textStorage?.append(myAttrString)
}
@objc func singleClickGesture(_ recognizer: NSClickGestureRecognizer) {
// Show cursor and set it to position on tapping
if recognizer.state == .ended {
textView.isEditable = true
textView.window?.makeFirstResponder(textView)
// Comment the next 9 lines to see the problem.
// TextView is not responding correctly to mouse clicks.
// The next lines fixes the problem for UIKit (iOS),
// but I couldn't make equivalent code for Cocoa (macOS).
let location = recognizer.location(in: textView)
if let position = textView.closestPosition(to: location) {
let uiTextRange = textView.textRange(from: position, to: position)
if let start = uiTextRange?.start, let end = uiTextRange?.end {
let loc = textView.offset(from: textView.beginningOfDocument, to: position)
let length = textView.offset(from: start, to: end)
textView.selectedRange = NSMakeRange(loc, length)
}
}
}
}
}解决办法?
发布于 2021-01-08 01:04:16
给你:
@objc func singleClickGesture(_ recognizer: NSClickGestureRecognizer) {
// Show cursor and set it to position on tapping
print("click \(String(describing: NSApp.currentEvent))")
if recognizer.state == .ended {
textView.isEditable = true
if let window = textView.window, window.makeFirstResponder(textView) {
let location = recognizer.location(in: textView)
let position = textView.characterIndexForInsertion(at: location)
textView.selectedRange = NSMakeRange(position, 0)
}
}
}https://stackoverflow.com/questions/65501428
复制相似问题