Devan
Devan

Reputation: 177

How to remove extra space from the top and bottom of the UITextview in iOS?

I want to remove the extra spaces between the top , bottom and in between the text. I've tried with this one, but it's removed all the new lines.

let newString = textView.text.replacingOccurrences(of: "\n", with: "")

Result - hellohow are youhow may i help you

Actual input:

hello

how are you

how may i help you  

Expected Result:

hello
how are you
how may i help you

Thanks!

Upvotes: 0

Views: 1458

Answers (2)

Haya Hashmat
Haya Hashmat

Reputation: 137

You can try this :

let new = yourTextView.text.trimmingCharacters(in : .whitespacesAndNewlines)

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

Method stringByTrimmingCharactersInSet returns a new string made by removing from both ends of the String characters contained in a given character set.

We can also just remove only whitespace, newline or both.

  let newString = textView.text.trimmingCharacters(in: .whitespacesAndNewlines)

Upvotes: 1

Related Questions