简体   繁体   中英

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!

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)

You can try this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM