iPhone: Disable the “double-tap spacebar for .” shortcut?

前端 未结 8 1746
陌清茗
陌清茗 2020-11-28 15:29

By default, if you tap the spacebar twice on the iPhone or iPad, instead of getting \"  \" (two spaces), you get \". \" (a period followed by a space). Is the

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 15:53

    A simpler solution that I have found to work for a UITextView is as follows:

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
        if ([text isEqualToString:@". "] && range.length == 1) {
            NSMutableAttributedString *attributedString = [textView.attributedText mutableCopy];
            [attributedString replaceCharactersInRange:range withString:@" "];
            textView.attributedText = attributedString;
            textView.selectedRange = NSMakeRange(range.location + 1, 0);
    
            return NO;
        }
        return YES;
    }
    

    This allows multiple spaces to be entered repeatedly and also handles attributed text. The only case I have found it to fail with is when pasting a period and space with a single character already selected.

提交回复
热议问题