Skip to content

SSYTokenField bug (infinite loop) if one token is larger than the available width #1

@francesco-romano

Description

@francesco-romano

I don't know if you have time to update this repository, but I found your SSYTokenField class very useful. I found a bug.. if you have a token which is larger than the availableWidth, the code simply tries to create a newline and postpone the insertion of the token in the nextline.. but this leads to an infinite loop...

I solved in this way

if (requiredWidth > availableWidth) {
            nRows++ ;
            if (nRows == 1) {
                // This would only happen if the first token by itself was too wide
                // to fit in a row, which would be very rare
                height += (tokenHeight + 2 * interrowSpace) ;
            }
            else {
                height += (tokenHeight + interrowSpace) ;
            }
            if (!nTokensInThisRow) {
                //This happens if one tag is too long to be added in one row, so
                //we want to add this tag "alone" in a row.. it will be truncated by the NSTokenField.
                //We now increase the height of the field by two rows
                height += (tokenHeight + interrowSpace); //counting another row for the too-long tag
                //we now count a fake row with one tag
                nRows++;
                nTokensInThisRow = 1;
                nTokensPlaced++;
            }

            startingToken = startingToken + nTokensInThisRow ;
            nTokensInThisRow = 0 ;
        }
        else {
            nTokensInThisRow++ ;
            nTokensPlaced++ ;
        }

The code I added is the following:

if (!nTokensInThisRow) {
                //This happens if one tag is too long to be added in one row, so
                //we want to add this tag "alone" in a row.. it will be truncated by the NSTokenField.
                //We now increase the height of the field by two rows
                height += (tokenHeight + interrowSpace); //counting another row for the too-long tag
                //we now count a fake row with one tag
                nRows++;
                nTokensInThisRow = 1;
                nTokensPlaced++;
            }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions