How to Limit the Number of Tokens in a NSTokenField?

大兔子大兔子 提交于 2019-12-10 20:37:01

问题


I have a NSTokenField Where the tokens are created upon hitting enter. I would like to limit the number of tokens in this field. Say for example, User should be allowed to enter only 2 tokens one after the other. Later, neither user should be allowed to set the Token nor user should be allowed to search further. In short, User should be blocked after 2 tokens.

Could any one please help me in achieving this???

Thanks in advance :)


回答1:


The solution is divided in 2 parts:

-(NSArray *)tokenField:(NSTokenField *)tokenField shouldAddObjects:(NSArray *)tokens atIndex:(NSUInteger)index
{
    //limit the tokens
    if(self.tokensLimit)
    {
        NSArray * tokensArray = [_tokenField objectValue];

        if([tokensArray count] > 0)
        {
            if([tokens isEqualToArray:tokensArray])
            {
                return tokens;
            }
            else if([tokensArray count]>=self.tokensLimit)
            {
                return @[];
            }
            else if([tokens count]>0)
            {
                tokens = [tokens subarrayWithRange:NSMakeRange(0, MIN([tokens
                                                                       count], self.tokensLimit))];
            }
            else
                return @[];
        }
        else
        {
            tokens = [tokens subarrayWithRange:NSMakeRange(0, MIN([tokens count], self.tokensLimit))];
        }
    }

    return tokens;
}

where tokensLimit is an int > 0 the delegate covers all the cases like tokens added by copy/paste, completion list, drag&drop, manually written etc..

this other delegate cover the case where the user write a string and hit "TAB"

- (BOOL)control:(NSControl *)control isValidObject:(id)object
{
    if(self.tokensLimit)
    {
        NSArray * tokensArray = [_tokenField objectValue];
        tokensArray = [tokensArray subarrayWithRange:NSMakeRange(0, MIN([tokensArray count], self.tokensLimit))];
        [_tokenField setObjectValue:tokensArray];
    }
    return YES;
}



回答2:


If you save the tokens in a db, you can count the number of rows of the particular users id, and add an if statement to limit it to 2.




回答3:


Behold:

var maximumTokens: Int = 2    
func tokenField(_ tokenField: NSTokenField, shouldAdd tokens: [Any], at index: Int) -> [Any] {
            var count = 0
    if let textView = tokenField.currentEditor() as? NSTextView {
        for scalar in textView.string.unicodeScalars {
            if scalar.value == unichar(NSAttachmentCharacter) {
                count += 1
            }
        }
    }

    return tokens.filter({ _ in
        count += 1
        return count <= maximimTokens
    })
}

I've tested it and it works when you are typing tags or even copying & pasting them in.



来源:https://stackoverflow.com/questions/17649986/how-to-limit-the-number-of-tokens-in-a-nstokenfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!