如何从我的 NSTextField 获取突出显示的文本?

发布于 2024-11-10 06:44:23 字数 169 浏览 4 评论 0原文

这肯定很容易,但我没有在文档中找到任何内容。 我需要的行为与右键单击文本的任何部分然后可以对其执行某些操作完全相同。 目前我有一个自己的自定义 NSTextField 类,它重新实现“mouseDown”操作。这部分有效!我以为我可以通过“theEvent”获得 nstextfield 的选定文本部分,但显然这是不可能的。

It must be certainly easy but I haven't found anything in the doc.
The behavior I need is exactly as when you right-click any portion of text and then you can do some action with it.
For the moment I have a my own custom NSTextField class which re-implment 'mouseDown' action. This part works ! I thought I could get the selected portion of text of my nstextfield thanks to 'theEvent' but apparently it is not possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

变身佩奇 2024-11-17 06:44:23

我实际上认为你不能用 NSTextField 做到这一点。我认为您必须改用 NSTextView,然后使用 -selectedRanges 方法。

编辑:我应该说,你不能直接执行此操作(即,没有 NSTextField 方法可以执行此操作)。我认为,您必须使用与窗口关联的字段编辑器(它本身就是一个 NSTextView)才能执行此操作。 这是苹果使用指南字段编辑器。

I don't actually think you can do this with NSTextField. I think you have to use an NSTextView instead and then make use of the -selectedRanges method.

EDIT: I should have said, you can't do this directly (i.e., there's no NSTextField method for doing this). I think rather, you have to use the field editor (which is itself an NSTextView) associated with the window in order to do this. Here's the apple guide for using the field editor.

你丑哭了我 2024-11-17 06:44:23
NSString *selectedText = ((NSTextFieldCell*)textField.selectedCell).stringValue;
NSString *selectedText = ((NSTextFieldCell*)textField.selectedCell).stringValue;
百思不得你姐 2024-11-17 06:44:23

添加到 @Sean 的答案,我使用以下解决方案来获取/更改 NSTextField 的选定文本:

- (BOOL) replaceSelectionOfField:(NSTextField *)textField withString:(NSString *)replacement {
    NSText * editor = [self.view.window fieldEditor:NO forObject:textField];
    if (editor) {
        NSRange rng = editor.selectedRange;
        if (rng.location != NSNotFound) {
            [editor replaceCharactersInRange:rng withString:replacement];
            return YES;
        }
    }
    return NO;
}

如果此函数返回 NO,则没有字段编辑器(文本字段可能没有焦点)或不选择。

Adding to @Sean 's answer, I use the following solution to get/change the selected text of an NSTextField:

- (BOOL) replaceSelectionOfField:(NSTextField *)textField withString:(NSString *)replacement {
    NSText * editor = [self.view.window fieldEditor:NO forObject:textField];
    if (editor) {
        NSRange rng = editor.selectedRange;
        if (rng.location != NSNotFound) {
            [editor replaceCharactersInRange:rng withString:replacement];
            return YES;
        }
    }
    return NO;
}

If this function returns NO, there is either no field editor (the text field may not have focus) or no selection.

拥有 2024-11-17 06:44:23

NSTextField 内部使用了 NSTextView,因此我们可以找到该文本视图,然后使用其公共 API 获取选择范围,然后计算所选文本。 (在 macOS 13.5.2 上测试)

extension NSTextField {
    
    func kjy_selectedText() -> String {
        guard let range = kjy_selectedRange(), range.length > 0 else { return "" }
        return (stringValue as NSString).substring(with: range)
    }
    
    func kjy_selectedRange() -> NSRange? {
        for subview in subviews {
            for subSubview in subview.subviews {
                if let textView = subSubview as? NSTextView,
                   let range = textView.selectedRanges.first?.rangeValue,
                   range.location != NSNotFound
                {
                    return range
                }
            }
        }
        return nil
    }
}

查看层次结构:

查看层次结构

NSTextField internally uses NSTextView, so we can just find that text view, then use its public API to get the selection range, then calculate the selected text. (Tested on macOS 13.5.2)

extension NSTextField {
    
    func kjy_selectedText() -> String {
        guard let range = kjy_selectedRange(), range.length > 0 else { return "" }
        return (stringValue as NSString).substring(with: range)
    }
    
    func kjy_selectedRange() -> NSRange? {
        for subview in subviews {
            for subSubview in subview.subviews {
                if let textView = subSubview as? NSTextView,
                   let range = textView.selectedRanges.first?.rangeValue,
                   range.location != NSNotFound
                {
                    return range
                }
            }
        }
        return nil
    }
}

View hierarchy:

View hierarchy

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文