(Mac OS X, Cocoa) How to make drag-and-droppable placeholder elements in an input text field? - macos

I'm writing a Cocoa app where I would like the user to be able to put together a template string using placeholders. (For example, an (artist) placeholder would be filled in by the artist of the song currently playing in iTunes, etc.) I've seen apps that do something like this where each possible placeholder term is displayed in a blue "lozenge," and the user can drag and drop these "lozenges" into an input text field to construct a string, optionally entering some custom text of their own (e.g. separating (artist) and (title) "lozenges" with a hyphen).
Does anyone know if there is any sample code anywhere that will help me implement something like this?
I'm talking about something like this: (this comes from the "Hazel" app where, in a Hazel rule, you can rename a file based on a template pattern you specify)

NSTokenField is focused in the above pic (has the blue ring around it). Each "token" (your lozenges) is an auto-recognized string for the token field. As rdelmar comments above, read up on NSTokenField and you'll be most of the way there.
The "source" of tokens is likely a rounded-edged NSBox containing lined-up borderless, no-background NSTokenFields with one token each. That'll give you easy drag-and-drop as well as easy alignment.

if you need to customize the l&f of the individual tokens, you need to implement your own stuff: NSTextView with NSTextAttachment which have NSTextAttachmentCells... Its painful and a lot of code but actually not that hard
The NSTokenAttachment cell only has lots of private l&f options :(

Related

NSSwitchButton alternatives

I am writing a Mac application that provides a "test" like function. This application (through a connection with a server). Basically the application will give the students a story to read, followed by a series of questions (also from the server) where the user can (attempt) to select the correct answers, and send the result back to the server to be verified.
Implementing the "story" part was easy. Just send all of the text to a NSTextView. I had been planning to implement the "select your answer" as programmatically created NSSwitchButtons. However, some of the possible answers might take up more than one line. I have not been able get (any) NSButton class to wrap text based on the frame size, and there doesn't seem to be an easy way to override NSButtonCell to allow the text to wrap.
What other Cocoa class(es) should I use to accomplish this task? I need to have a check-box interface (so that people can select one or more possible answers, and the answers can be an arbitrary length - within reason!) Ideally it would also be easy to use so that it will be easy to programatically layout the answers as well. (Some problems may only have 2 choices, while others may have 5+) I can't imagine I'm the only one who needs this type of functionality
(Oh...since a picture is worth 1,000 words, I've attached a screen-shot of my app below with some answer text running off of the screen)
An NSButton will respect explicit linefeed characters embedded in the text, but I suppose that would not meet your needs. An alternative would be to have a static text item next to a checkbox with no title. Of course, if you want to be able to toggle the checkbox by clicking the text, you would have a little more programming to do.

Japanese input control

I have the following issue, concerning the way to fine control Japanese input in an iOS app. To make things clear I include an image to this post (see below).
As one can see I have typed や in the input text field and a bunch of choices (や 役 矢 訳 ヤ 屋 焼き …) appear above the keyboard for possible conversions.
Here is what I would like to know:
The choices presented in the picture make no sense for my particular app. I would like to be able to set myself the possible conversions offered to the user.
In this case it would be (山形県 山口県 山梨県). Does anyone know how I can achieve this?
In other words I am trying to find out how handle the set of data appearing above the keyboard.
Thanks in advance for any relevant information.

NSTextView / NSScrollView - A few questions to help me understand it's proper usage

I have created a "notes" field designed to hold multiple paragraphs of text which I would like to store in a custom object. Originally, I just used an NSTextField as a temporary solution, but this does not allow me to scroll or have multiple paragraphs of text...
In IB I have placed a NSTextView (which seems to be wrapped inside an NSScrollView.) Upon execution of my program, seems to allow me to enter text in multiple paragraphs, scroll, etc. In short it LOOKS to be exactly what I want would like it to be. So far so good.
Now, I need to retrieve the data from this field and store it in my custom object. This is where I'm getting a bit lost within the developer documentation...
My goals are fairly straight forward:
Allow users to type away in the box.
Store the contents of the box into a variable (array, etc.) in my custom object when the user moves to another field, leaving the notes field.
Display the users stored text in the text box next time the record is viewed.
Second, is there a simple way to retrieve and store the data into a "notes" variable in my custom object (such as an NSString object? I would think having multiple would exclude an NSString object as an option here, but maybe I'm wrong) or am I getting into a more complex area here (such as having to store it in an array of NSString objects, etc.)?
Any help would be appreciated!
You can get the data using -string, defined by NSText (e.g. NSString *savedString = [aTextView string])
Your save code can be put in your NSTextDelegate (read, delegate of the NSTextView, because it's the immediate superclass), in – textDidEndEditing: which will be called, well, when editing is finished (e.g. when the user clicks outside the view) or one of the other methods.
Then to reload the saved string if you emptied the text view or something, use [textView setString:savedString] before editing begins.
NSTextDelegate documentation: here.
I'm not sure what you mena when you say "store the contents of the box into a variable (array, etc.) Are you hoping for an array of custom notes? Text views store a string of data, so the easiest way of storing its value is using one string; if you need an array of notes you'd have to split the string value into different paragraphs, which shouldn't be too hard.

How to write long formatted text in a Text View with xCode

I feel strange asking a question about what's probably the simplest page in my iPhone app but here it is anyway:
I have a controller that a user can invoke to display the rules of the game and some acknowledgment information. I set up a Text View object filling up the whole page in Interface Builder and am wondering what's the best way of entering the text I need.
I can do all of this in the m file but entering large text sections in a programming language is never fun.
I can also simply overwrite the default "Lorem ipsum dolor..." text in Interface Builder but then my return characters don't seem to be taken into account when I run the app.
Is there a better way to fill my Text View with my own formatted text? And how can I format my text neatly and easily (i.e. make titles in bold, underline some words, etc.)?
Thanks.
There is no way to easily display formatted text in a UITextView. The best approach for this kind of problem is to use a UIWebView and store the text as an HTML file.
Use the NSTextStorage class to store the formatted text in your NSTextView.
Use Core Text
Core Text Tutorial for iOS: Making a Magazine App

cocoa + what ui element should i use?

I am developing an app which has a text field box. so when i am entering text, it should search the database and give me suggestions just like google search. like if i have entered letter 'a', it should have a box like thing below text field with all names starting with letter 'a'. then if i have entered letter 'b', it should resize the box and give names that start with 'ab' and so on.
I was planning to use a table view below the text field which reloads itself as and when new thing is entered into text field. but i dont know how to resize the table view depending on number of suggestions. So is there any other ui element which suits this kind of situations?
Also how do i detect a new alphabet or number entered into text field so that i can filter out suggestions like is there any such notification? what i mean to say is say i entered letter 'a' so now the string value of text field is 'a' and now i entered say 'b'. so now the string value changed to 'ab'. how do i detect this? i think textDidChange notification will do this for me.
Thanks
You can use an NSComboBox for auto-completing lists like you describe. NSComboBox is a subclass of NSControl so you can use the -controlTextDidChange: delegate method to detect changes in the text that the user types. Make sure you set the control to "continuous" in Interface Builder or call [comboBox setContinuous:YES].
If, for whatever reason, you find that a combo box is inappropriate for your situation, you can implement the list of completions as a child window of the control's window, with a headerless table view in it. Then you would programmatically resize that window as the number of possible completions changes.

Resources