Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 959641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:03:31+00:00 2026-05-16T01:03:31+00:00

I have a button that adds a token to a NSTokenField. It adds the

  • 0

I have a button that adds a token to a NSTokenField. It adds the token always to the end of the field:

NSTokenField *currentField = [sender representedObject];

    // Determine which token should be inserted into the field using the tag of the sender.
    switch( [sender tag] )
    {
        case eFileNameToken_StartDate:
            [currentField setObjectValue:[[currentField objectValue] arrayByAddingObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:kTokenName_StartDate, kTokenKey_Name, @"%m-%d-%Y", kTokenKey_Format, [NSNumber numberWithInt:0], kTokenKey_FormatIndex, nil]]];
            break;

Because it is grabbing the array from the currentField objectValue and then creating an array by adding the object.

I would love to have it know the insertion point of the cursor and insert the object into the resulting currentField objectValue so that I can then setObjectValue of the currentField with the correctly ordered tokens.
Thanks for any help yall

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:03:32+00:00Added an answer on May 16, 2026 at 1:03 am

    So I figured it out and I thought Id share my solution.

    One needs to grab the fieldEditor and check its selectedRange.

    NSText *textEditor = [currentField currentEditor];
    

    This gives you the insertion point in the currently edited text field. However if you have a tokenField that has text and tokens mixed together then you will find that each token only counts as one character in the selectedRange.

    If this is the case then you need to write some logic to correctly insert into the currentField array.

    1. Steps:
      1. make a temp NSMutableArray
      2. fill that with the [currentField objectValue]
      3. iterate through this temp array, incrementing a postion counter, 1
        for classes of kind token, 1 for
        each character in a non token string
      4. at the end of each loop increment your array index
      5. make sure to check if the positioncounter is equal or greater
        than your [currentFieldEditor
        selectedRange].location and break
        out of the loop
      6. Finally insert your new token into the temp array and then
        [currentField setObjectValue: with
        that array]

    My tokens are comma delinated and shoved into NSDictionaries with $token$ style names
    This is how I delineate between text and tokens when I run my loop.

    happy 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jQuery question: I have an ADD button that adds text to
Basically what I have here is a button that adds a new list item
I have a panel and a button that adds text boxes to the panel
Hi I have a button (btnAdd) that adds the content of a textbox (txtName)
I have a button that, when tapped, adds a label inside a view. Anytime
I have a MapView with a toolbar button that when pushed adds overlays to
I've managed to create a button that adds a file upload field, a button
I have some code below that creates a RelativeLayout and adds a button to
I have a JavaScript function that adds a more button to a div that
I have one button, and when i click it it adds elements that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.