Oliver Leopold
Oliver Leopold

Reputation: 33

UITextView Detect Line Breaks

I have a UITextView. I want to be able to detect every single line break and replace it with other content that can be decoded later. So, if there is

Hey

Tim

it would automatically encode it to something like:

Hey%29Tim

Then, there would be another way to decode it back.

Is that possible? I know it is not that clear, but I don't understand how to explain it. So, if you have questions, ask below!

Upvotes: 1

Views: 308

Answers (1)

dfrib
dfrib

Reputation: 73176

The text in a UITextView is just a String instance, where line breaks are represented by escape character \n. Hence, you could read the .text property of your UITextView instance and replace it with a string where you've replaced the escape character \n with whatever you like; in your case %29.

Example:

var myString = "Hey\nTim"
print(myString)
/* Hey
   Tim  */

myString = myString.stringByReplacingOccurrencesOfString("\n", withString: "%29")
print(myString)
/* Hey%29Tim */

Upvotes: 1

Related Questions