【问题标题】:Xcode parse UITextView (multiple line breaks)Xcode解析UITextView(多行换行)
【发布时间】:2016-04-17 11:53:39
【问题描述】:

请问有人可以帮我解决以下问题吗?如果用户在 UITextView 中写入项目符号文本(多个换行符),如何检测换行符的总数?

字符\n(换行符)没有出现在UITextView的内容中。

【问题讨论】:

    标签: xcode uitextview line-breaks


    【解决方案1】:

    即使\n 字符不会出现在控制台上,如果您使用print(textView.text),它仍然存在于text 属性上。

    计算换行数的一种方法是使用\n 分割字符串并计算块数 - 1。

    let numberOfLineBreaks = textView.text.componentsSeparatedByString("\n").count - 1
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      Raphael Oliveira 的答案不是我们想要的答案。

      我找到了!

      神奇的换行符是\ \ 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;
      

      【讨论】:

        【解决方案3】:

        您可以尝试使用 NSCharacterSet.newlineCharacterSet() 而不仅仅是 "\n" 在“换行符集”中查找字符

        【讨论】:

          猜你喜欢
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多