防止在shouldChangeTextInRange期间删除前缀 [英] Prevent prefix from deleting during shouldChangeTextInRange

查看:73
本文介绍了防止在shouldChangeTextInRange期间删除前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITextView ,它的前缀包含一个4个字符的空格字符串作为缩进。如果我键入很多文本,然后按住退格键约一秒钟,它将快速逐字删除文本,但同时也会删除我的分隔符空间,这会导致我的 UITextView 被卡住,无法再输入。

I've got a UITextView that has a prefix containing a 4 character whitespace string as an indent. If I type a lot of text and then hold the backspace button down for about a second, it deletes text at a rapid pace word by word, but it also deletes my "seperator space" which then causes my UITextView to be stuck and not able to type anymore.

这就是我要谈论的问题:

This is the catch that I'm talking about:

if (range.location <= 4 && textView == self.descriptionTextView) {
    #warning fast deletion causes this to be un-editable
    return NO; // makes sure no one can edit the first 4 chars
}

如何防止

推荐答案

为了维护您的前缀,我d建议弄清楚如果实际上要更改给定范围内的字符会导致的字符串,然后仅当前缀不按原样保留时才允许更改文本;例如,在您的特定情况下:

In order to maintain your prefix, I'd recommend figuring out the string that would result if you were to in fact change the characters in the given range, and then only allow for the text to change if the prefix doesn't remain as is; for example, in your specific case:

- (BOOL)textView:(UITextView *)textView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    // Combine the new text with the old
    NSString *combinedText = [textView.text stringByReplacingCharactersInRange:range withString:string];

    // If the user attempts to delete before the 4th
    // character, delete all but the first 4 characters
    if (combinedText.length < 4) {
        textView.text = @"    "; // <-- or whatever the first 4 characters are
        return NO;
    }

    // Else if the user attempts to change any of the first
    // 4 characters, don't let them
    else if (![[textView.text substringToIndex:4] isEqualToString:[combinedText substringToIndex:4]]) {
        return NO;
    }

    return YES;
}

或更笼统地说,为了灵活起见,您可以存储前缀字符串作为类实例变量,然后将 shouldChangeCharactersInRange:代码作为前缀变量的基础:

Or more generally speaking, to allow for flexibility, you can store your prefix string as a class instance variable then base your shouldChangeCharactersInRange: code on whatever that prefix variable may be:

- (BOOL)textView:(UITextView *)textView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    // Combine the new text with the old
    NSString *combinedText = [textView.text stringByReplacingCharactersInRange:range withString:string];

    // If the user attempts to delete into the prefix
    // character, delete all but the prefix
    if (combinedText.length < self.prefixString.length) {
        textView.text = self.prefixString;
        return NO;
    }

    // Else if the user attempts to change any of the prefix,
    // don't let them
    else if (![self.prefixString isEqualToString:[combinedText substringToIndex:self.prefixString.length]]) {
        return NO;
    }

    return YES;
}

这篇关于防止在shouldChangeTextInRange期间删除前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆