NSTokenField with mixed token/string input, possible?

后端 未结 2 1505
一整个雨季
一整个雨季 2020-12-08 22:34

When entering an invalid email in Mail\'s NSTokenField one get\'s this (a mix of token and plain string values):

<

2条回答
  •  萌比男神i
    2020-12-08 23:17

    You need to implement the delegate method tokenField:styleForRepresentedObject: to return either NSRoundedTokenStyle for tokens or NSPlainTextTokenStyle for other text. The represented object for an token is the token string itself, unless your delegate returns other objects.

    This should do the trick for your case:

    - (NSTokenStyle)tokenField:(NSTokenField *)tokenField
     styleForRepresentedObject:(id)representedObject
    {
        if ([representedObject rangeOfString: @"%["].location == 0) {
            return NSRoundedTokenStyle;
        } else {
            return NSPlainTextTokenStyle;
        }
    }
    

提交回复
热议问题