有没有人能帮我回答下面这个问题?如果用户在UITextView中编写项目符号文本(多个换行符),如何检测换行符总数?
字符\n (换行符)未出现在UITextView的内容中。
发布于 2016-01-22 07:57:44
即使\n字符没有出现在控制台上,如果您使用print(textView.text),它仍然在text属性上。
计算有多少换行符的一种方法是使用\n拆分字符串,并计算组块的数量- 1。
let numberOfLineBreaks = textView.text.componentsSeparatedByString("\n").count - 1我希望这能有所帮助。
发布于 2016-01-22 08:11:23
拉斐尔·奥利维拉的回答这不是理想的答案。
我找到了!
神奇的换行字符是\\ n。
代码是:
NSString *string = [object valueForKey:@"textViewContent"];
NSError *error = nil;
NSRegularExpression *regularExpression =
[NSRegularExpression regularExpressionWithPattern:@"\\n"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSUInteger numberOfMatches = [regularExpression numberOfMatchesInString:string
options:0
range:NSMakeRange(0,[string length])];
NSLog(@"%lu",(unsigned long)numberOfMatches);
NSUInteger lines = numberOfMatches;发布于 2016-04-17 11:53:39
您可以尝试使用NSCharacterSet.newlineCharacterSet()而不只是"\n"在‘新行字符集’中查找字符
https://stackoverflow.com/questions/34936161
复制相似问题