ios 如何删除 UITextView 中的行空格

How to remove line spaces in UITextView in ios

我有一个 textView,因为我正在输入数据,然后按下回车键,它会转到下一行,我输入了一些其他数据并保存了它。

我得到的结果是:

它存储在像这样的服务中:

{"PersonID":9416,"PeriodID":59130,"Notes":"tyyyttyt
ttrtrtrt
ttttrtrtr","PeriodDate":"2015-05-08T10:50:00"} 

但我的要求是字符串应存储为:

tyyyyttyt ttrttrt ttttrtrtr

我在 google 中搜索过,但我没有得到答案。

谁能帮我解决这个问题?

我对 Objective C 个主题很陌生。

要从字符串中删除新行,请使用以下代码:

myString = [myString stringByReplacingOccurrencesOfString:@"\n" withString:@" "];

其中 myString 是您从 UITextView 中获取的文本。

希望对您有所帮助!

将您的字符串分隔成由任何换行符分隔的组件,然后再次将它们连接到一个新字符串中,用 space:

分隔它们
NSString *cleanString = [[myTextField.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];

试试吧。将 Double Space 替换为 Single space.

for (int i=0; i<[self.textView.text length]; i++) 
{
   self.textView.text=[self.textView.text stringByReplacingOccurrencesOfString:@"  " withString:@" "];

}