iOS Token (tag) Field

I’m looking for a way to simplify the user’s input, by using a token (or tag) field.

Something similar like this:

Does something like this already exist in the iOS framework?
If so, how would I implement something like that?
If not, how would something like this be made in Xojo?

I know MBS has the NSTokenFieldControlMBS. But that is not (yet) iOS compatible. :confused:

GraffitiTagField?

2 Likes

No tokens or tags, but you can add a search field to a table.

Yeah, I think that’s what I might be looking for.

I looked into the Graffiti package before. And for now it is the subscription model that held me back.
Due to some health issues (post covid / post Lyme / etc), I can’t work as much as I used to. So, I have to watch my budget.

If I had more software consulting jobs, I could consider investing more in extra plug-ins.

That’s what I do now. But a tag / token field looks neater :smiling_face:

UISearchTextField allows you do to this: tokens | Apple Developer Documentation but im not sure anyone has a declare/plugin exposing it

They should be fairly easy to implement. They’re just strings with an optional icon.

So I did a little experimentation with an iOSMobileTable and the problem is that the search field triggers the SearchChanged event on every keystroke. After a quick skim this morning, I don’t see a way to only execute code when the user presses the Search button.

Here’s the code for adding a token as an extension method:

Public Sub AddSearchTokenAt(extends t as iOSMobileTable, index as integer, name as string, icon as picture = nil)
  If name = "" Then
    Return
  End If
  
  If name.IndexOf(Chr(13)) = -1 Then
    Break
    Return
  End If
  
  // @property(nonatomic, strong, readonly) UISearchBar *searchBar;
  Declare Function getSearchBar Lib "Foundation" Selector "searchBar" (obj As ptr) As Ptr
  
  Dim searchBar As ptr = getSearchBar(t.SearchControllerHandle)
  
  // @property(nonatomic, readonly) UISearchTextField *searchTextField;
  Declare Function getSearchTextField Lib "Foundation" Selector "searchTextField" (obj As ptr) As Ptr
  
  Dim field As ptr = getSearchTextField(searchBar)
  
  
  // - (void)insertToken:(UISearchToken *)token atIndex:(NSInteger)tokenIndex;
  Declare Sub insertToken_atIndex Lib "Foundation" Selector "insertToken:atIndex:" ( obj As ptr , token As Ptr , tokenIndex As Integer )
  
  Declare Function NSClassFromString Lib "Foundation" (name As cfstringref) As ptr
  
  // + (UISearchToken *)tokenWithIcon:(UIImage *)icon text:(NSString *)text;
  Declare Function tokenWithIcon_text Lib "Foundation" Selector "tokenWithIcon:text:" ( cls As ptr , icon As Ptr , Text As CFStringRef ) As Ptr
  
  Dim UISearchToken As Ptr = NSClassFromString("UISearchToken")
  Dim token As ptr = tokenWithIcon_text(UISearchToken, Nil, name)
  
  // Set the text to "" so we don't recurse
  // @property(nullable, nonatomic, copy) NSString *text;
  Declare Sub setText Lib "Foundation" Selector "setText:" (obj As ptr, value As CFStringRef)
  setText(field, "")
  
  // add the token at the specified index
  insertToken_atIndex(field, token, index)
  
End Sub
1 Like