Reputation: 38162
I have a UITextField where in I am restricting entering more than 2 digits. Now, it works fine but when I tap on the text field & select all the content & then type it does not allow me to over-write the existing 2 digits.
Any clue on how to use 'selectedTextRange' property here?
- (BOOL)cell:(RunnerTableViewCell *)iCell shouldChangeCharactersInRange:(NSRange)iRange replacementString:(NSString *)iString {
self.navigationItem.rightBarButtonItem.enabled = YES;
NSCharacterSet *anUnacceptedInput = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
int aCharacterUpperLimit = 2;
BOOL aReturnValue = YES;
NSInteger aTotalLength = iCell.inputField.text.length + [iString length];
NSLog(@"iString=%@ aTotalLength=%d",iString,aTotalLength);
if ([[iString componentsSeparatedByCharactersInSet:anUnacceptedInput] count] > 1 || (aTotalLength > aCharacterUpperLimit && ![iString isEqualToString:kRunnerEmptyString]) || [iString length] > aCharacterUpperLimit) {
aReturnValue = NO;
}
return aReturnValue;
}
Upvotes: 1
Views: 218
Reputation: 57149
Try this:
NSInteger aTotalLength = iCell.inputField.text.length + [iString length] - iRange.length;
Upvotes: 3