UITextView 中的两个空格会自动插入一个 . iPhone 中的文本后(句号)

发布于 2024-11-26 17:31:32 字数 190 浏览 0 评论 0原文

我有一个 UITextView,在其中插入逗号分隔值。但是当我插入任何文本并在其后给出两个或更多空格时。它会自动在最后一个文本后添加句号。

是手机设置的原因吗?即使是。我们该如何预防呢?

编辑要重现它..在 UITextView 中输入任何单词,然后尝试应用两个空格。它会自动在单词末尾添加句号:)

I have a UITextView where I am inserting comma separated values. but when ever I insert any text and gives two or more space after it. It automatically appends the FULL STOP after last text.

Is it due to Phone setting? Even if yes. How can we prevent it?

EDIT To reproduce it.. Enter any word in UITextView and then try to apply two spaces. It will automatically add fullstop at the end of hte word :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦境 2024-12-03 17:31:32

这是因为设置'。'键盘设置中的快捷方式

It's because of the setting '.' shortcut in keyboard settings.

千年*琉璃梦 2024-12-03 17:31:32

苹果将​​该行为描述为:“双击空格键将插入一个句点,后跟一个空格”,而这确实是一个 iOS 系统设置(设置/常规/键盘/“。”快捷方式)。

用户可以禁用该行为,但通过特定 UITextView 上的代码禁用它似乎很困难 - 请参阅 iPhone:禁用“双击空格键。”快捷方式?

Apple describes the behaviour as: "Double tapping the space bar will insert a period followed by a space", and it is indeed an iOS system setting (Settings/General/Keyboard/"." Shortcut).

The user can disable the behaviour, but disabling it by code on a specific UITextView seems difficult - see iPhone: Disable the "double-tap spacebar for ." shortcut?

夜吻♂芭芘 2024-12-03 17:31:32
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//Check for double space
return !(range.location > 0 &&
         [text length] > 0 &&
         [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[text characterAtIndex:0]] &&
         [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textView text] characterAtIndex:range.location - 1]]);

}

上面的代码将限制用户输入多个空格。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
//Check for double space
return !(range.location > 0 &&
         [text length] > 0 &&
         [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[text characterAtIndex:0]] &&
         [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textView text] characterAtIndex:range.location - 1]]);

}

Above code will limit user to enter more than one blank space.

时光病人 2024-12-03 17:31:32

你需要检查前一个和替换的字符是否都是空的,并且自己而不是iOS进行替换,才有机会获得第二个空的空间。

从处理程序返回 false 可实现以下目的:

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool 
    {
    
    guard let textViewText = textView.text, range.location > 0 else {
      return true
    }
    
    let previousCharIndex = textViewText.index(textViewText.startIndex, offsetBy: range.location - 1)
    let previousChar = textViewText[previousCharIndex]
    
    if previousChar == " " && text == " " {
      let insertionPoint = textViewText.index(textViewText.startIndex, offsetBy: range.location)
      textView.text?.insert(" ", at: insertionPoint)
      return false
    }
    
    return true 
    }

You need to check if the previous and the replacement characters are both empty space, and do the replacement yourself instead of iOS to have a chance to get the second empty space.

Returning false from the handler achieves this:

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool 
    {
    
    guard let textViewText = textView.text, range.location > 0 else {
      return true
    }
    
    let previousCharIndex = textViewText.index(textViewText.startIndex, offsetBy: range.location - 1)
    let previousChar = textViewText[previousCharIndex]
    
    if previousChar == " " && text == " " {
      let insertionPoint = textViewText.index(textViewText.startIndex, offsetBy: range.location)
      textView.text?.insert(" ", at: insertionPoint)
      return false
    }
    
    return true 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文