首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在NSTextView中显示光标,单击location - MacOS

如何在NSTextView中显示光标,单击location - MacOS
EN

Stack Overflow用户
提问于 2020-12-30 02:51:43
回答 1查看 370关注 0票数 2

我正在开发一个macOS应用程序。我想要编写一个代码来显示NSTextView中单击位置上的光标。所以我在StackOverFlow搜索发现了the following code for iOS

代码语言:javascript
复制
@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

  • Change

  • UITapGestureRecognizer更改为textView.becomeFirstResponder()textView.window?.makeFirstResponder(textView)

问题是:

我无法将方法.closestPosition.textRange.offset转换为macOS,因为它们只在UIKit中可用,并且它们返回UITextPositionUITextRange等UIKit值。

最小可重现性示例

要重现这个问题,只需创建一个基于文档的Cocoa应用程序(macOS),并添加一个可滚动的TextView,那么代码如下:

代码语言:javascript
复制
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)
                }
            }
        }
    }

}

解决办法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-08 01:04:16

给你:

代码语言:javascript
复制
@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)
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65501428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档