Click on NSTextField Label to emulate hyperlink to a folder

前端 未结 3 1438
[愿得一人]
[愿得一人] 2021-01-12 10:44

I have an NSTextField label and I want something to happen when a user clicks on it.

I thought I could create an IBAction and link this to the label but nothing seem

相关标签:
3条回答
  • 2021-01-12 11:38

    You've got a couple options. Francis's answer is one option. Another option is to subclass NSTextField and override -mouseDown:. Something like this (written off the top of my head, not tested):

    @interface ClickableTextField : NSTextField
    @end
    
    @implementation ClickableTextField
    
    - (void)mouseDown:(NSEvent *)theEvent
    {
        [self sendAction:[self action] to:[self target]];
    }
    
    @end
    

    If NSTextField is closer in style to the appearance you need, this might be the better approach. If you need NSButton's features (highlight upon click, etc) go with Francis's solution.

    0 讨论(0)
  • 2021-01-12 11:45

    Another option is to create a transparent button (without title/image/border, momentary push in) in front of the label, with the same frame as label's. And set button's action accordingly.

    0 讨论(0)
  • 2021-01-12 11:50

    Labels are non-editable text fields thus don't send actions to their targets. You want to use an NSButton and turn off its border-drawing and size it to fit its text as best as possible to simulate a label.

    0 讨论(0)
提交回复
热议问题