Highlight string in text field and remove it at once

Hello everyone,


I have following use case:

I have a search bar to query my database depending on the input of the user. The results will be person data information displayed within a table view. Whenever a user clicks on a specific record, the name of the person object should be added to another textview but kind of highlighted.

To be more clear, let's say, I select "Nazar Medeiros", then ideally the whole string "Nazar Medeiros" should be used as one long string.

In case, I decide to remove it from the text view, then the whole name "Nazar Medeiros" should be removed at once and not with deleting each character one by one. Highlighting could mean having an underline for example.


Can you follow me? 🙂


Unfortunately, I don't have any code to show because I still don't know where to start.

You will select persons one by one in my case and all the names should appear on that text view like:


Nazar Medeiros, Kaan Cayoglu


After selecting a person from the table view, I also want to add the object into an array of persons. If the highlighted value is removed, the corresponding object should be removed from the array as well.

Replies

Can you follow me?

No. Please try to show some code, you know you want to use the text view, then you can show some code.

>Unfortunately, I don't have any code to show because I still don't know where to start.


In that case, you shouldmove this to the'Getting Started' forum. Asking in this specific forum assumes you possess at least some code, and are capable of grasping replies without handholding or being spoon fed.


Sounds like you've been given a specification, but have no experience with making apps....


Good luck.

To highlight, you could just set attributes with underline to each string of name.


To delete the whole name, when you select it by typing inside or just at the end, set the selection to the name (you could find the range by finding positions of commas, or keep track of them in an array…


Have a look here

https://stackoverflow.com/questions/34922331/getting-and-setting-cursor-position-of-uitextfield-and-uitextview-in-swift

Highlighting could mean having an underline for example.

Highlighting text like this can be a bit tricky. WWDC 2018 Session 201 Creating Apps for a Global Audience goes into this in detail, but the summary is:

  • Bold works well for highlighting works

  • Different text colours work well for highlight characters

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"