Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make portion of UITextView undeletable

I have a UITextView and need to make a specific portion un-deletable. Its the first 10 characters of the views text.

I just want it so that if the user is tapping the delete key on the keyboard it simply stops when it reaches say the 10th character in.

Edit

Let me go into a bit more detail.

Let's say the prefix is '123456789:'. I want to be able to type anywhere after this prefix, it can't be editable at all though, so '123456789:' shouldn't not be altered at all. Fichek's answer does this perfectly, however the prefix isn't always there, so how can I detect when it isn't in the textview? I thought the if statement did this but it seems not to.

like image 312
Josh Kahane Avatar asked Feb 25 '12 14:02

Josh Kahane


2 Answers

You can use the delegate method textView:shouldChangeTextInRange:replacementText: To tell the text view whether to accept the delete or not.

As the documentation says:

range : The current selection range. If the length of the range is 0, range reflects the current insertion point. If the user presses the Delete key, the length of the range is 1 and an empty string object replaces that single character.

Edit

Here is an implementation where the user can't delete the the first ten characters. But he will be able to insert characters there.

- (BOOL)textView:(UITextView *)textView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.length==1 && string.length == 0) {
        // Deleting text
        if (range.location <= 9) {
            return NO;
        }
    }
    return YES;
}

Here is an implementation where he can't modify the first ten characters at all.

- (BOOL)textView:(UITextView *)textView shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location <= 9) {
        return NO;
    }
    return YES;
}
like image 98
sch Avatar answered Sep 21 '22 00:09

sch


sch's last edit makes a decent answer, but I want to offer a slightly more flexible approach.

You have to keep in mind the copy/paste system. User might select all the text in text field and try to paste in the entire value which might be perfectly acceptable, but if (range.location <= 9) { return NO; } will reject it. The way I'd do it is put together a string that would be a result of successful edit and then check if that string would start with your desired prefix.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSLog(@"resulting string would be: %@", resultString);
    NSString *prefixString = @"blabla";
    NSRange prefixStringRange = [resultString rangeOfString:prefixString];
    if (prefixStringRange.location == 0) {
        // prefix found at the beginning of result string
        return YES;
    }
    return NO;
}

Edit: if you want to check if the current string in text field starts with the prefix, you can use rangeOfString: the same way:

NSRange prefixRange = [textField.text rangeOfString:prefixString];
if (prefixRange.location == 0) {
    // prefix found at the beginning of text field
}
like image 32
Filip Radelic Avatar answered Sep 22 '22 00:09

Filip Radelic