Search code examples
swiftmacoscocoanssearchfield

How do I do Apple Finder -> File Info -> add tag feature in MacOSX app?


in my MacOSX app, I need to create something similar to Apple's own add a tag to a file feature within the file's info screen. Right now, I've created an NSTextField where the user can type the text which calls:

func controlTextDidChange(aNotification: NSNotification) 

within this method I am showing an NSTableView directly below the NSTextField. I have an array that contains the content loaded in the tableview. I'm not using NSSearchField as I don't want the magnifying icon within the box. Design also calls for the chosen tags to become little buttons like in Apple's design either within same textField or directly beneath. Is there anything already built into the framework to do this easily or anything 3rd party? I've been having trouble searching without most of the results trying to teach me how to create tags for a file.

I have the string for the selected item in tableview then become a title for a button within the space below textfield.

The 2nd part of this design is deleting the tag buttons. Right now, I'm setting highlight status to true when button is selected and then detecting delete key with:

override func keyUp(theEvent: NSEvent) {
if theEvent.keyCode == 51 {
  if appDelegate.uploaderController.tagButton.highlighted == true {
    appDelegate.uploaderController.tagButton.hidden = true
  } 
}

if tagButton is highlighted when delete key is tapped, the button is hidden (may switch to deleted).

Again, if anyone has better ideas on design, know of anything built-in, or even done already, please let me know! Thanks!

File Tags


Solution

  • The best starting point is probably NSTokenField. That will give you autocomplete (using a little drop-down menu below the field). Check out NSTokenFieldDelegate's tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:. It'll also make tags into little oval objects you can drag around, delete, etc.

    NSTokenField reference