user2636197
user2636197

Reputation: 4122

swift uitextview limit number of line breaks to two at the time

I wonder if its possible to limit the number of line break that you can do in row?

I have set a limit to the number of characters that can be inserted but users can still insert text like:

Sometext
(line break) 
(line break) 
(line break) 
(line break) 
(line break) 
(line break) 
some more text...

Is it possible to only low two line breaks at the time? So that the above text ends up like:

Some text
(line break)
(line break)
some more text...

Since I cant edit the text backend I must do it in the fronend.

I want too clean out the text and limit the amount of line breaks in a row to two after the user has finished typing

Upvotes: 0

Views: 435

Answers (1)

SrB
SrB

Reputation: 363

As long as there are 3 newline characters in a row in the text in the UITextView, you can replace them with only 2 newline characters.

while str.contains("\n\n\n") {
    str = str.replacingOccurrences(of: "\n\n\n", with: "\n\n")
}

Upvotes: 2

Related Questions