Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中删除UITextview顶部和底部的额外空间?_Ios_Swift_Iphone_Uitextview - Fatal编程技术网

如何在iOS中删除UITextview顶部和底部的额外空间?

如何在iOS中删除UITextview顶部和底部的额外空间?,ios,swift,iphone,uitextview,Ios,Swift,Iphone,Uitextview,我想删除顶部、底部和文本之间的额外空格。 我试过用这个,但它去掉了所有的新线 let newString = textView.text.replacingOccurrences(of: "\n", with: "") 结果-你好,我能为您做些什么 实际投入: hello how are you how may i help you 预期结果: hello how are you how may i help you 谢谢 方法返回通过从给定字符集中包含的字符串字符的两端删除而生成

我想删除顶部、底部和文本之间的额外空格。 我试过用这个,但它去掉了所有的新线

let newString = textView.text.replacingOccurrences(of: "\n", with: "")
结果-你好,我能为您做些什么

实际投入:

hello

how are you

how may i help you  
预期结果:

hello
how are you
how may i help you
谢谢

方法返回通过从给定字符集中包含的字符串字符的两端删除而生成的新字符串

我们也可以只删除空格、换行符或两者

  let newString = textView.text.trimmingCharacters(in: .whitespacesAndNewlines)
您可以尝试以下方法:


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

它仅在模拟器和设备中工作,其行为与此textView.text.replacingOccurrences相同(of:“\n”,with:”)。知道为什么它不能在设备中工作吗。Thanks@Devan-删除应用程序并再次安装“忘记接受你的答案,伙计”。现在接受了。谢谢你的帮助。