phoenix-iphone-developer-group

[Prev] Thread [Next]  |  [Prev] Date [Next]

[Pi] Re: How to get UITextField to stop auto-adding periods to words when two spaces are typed. Seth Kromholz Thu Feb 05 19:00:20 2009


As another method -- albeit non-recommended and hackish -- a friend  
suggested this:

- (BOOL)textField:(UITextField *)textField  
shouldChangeCharactersInRange:(NSRange)range replacementString: 
(NSString *)text {
     if ([lastReplacementString isEqualToString:@" "] && [text  
isEqualToString:lastReplacementString]) {
         // Double space; clear period on the next runloop iteration  
after current edit (it will never appear on screen)
         lastRange.length = 1;
         self.lastReplacementString = nil;
         [self  
performSelectorOnMainThread:@selector(searchForAutoPeriodInTextField:)  
withObject:textField waitUntilDone:NO];
     } else {
         self.lastReplacementString = text;
         lastRange = range;
     }
     return YES;
}

- (void)searchForAutoPeriodInTextField:(UITextField *)textField {
     NSString *period = [textField.text substringWithRange:lastRange];
     NSLog(@"Supposed auto-period at %@ (%@)",  
NSStringFromRange(lastRange), period);
     if ([period isEqualToString:@"."]) {
         if (lastRange.location == 0) lastRange.location = 1;
         NSString *correctedString = [textField.text  
stringByReplacingCharactersInRange:lastRange withString:@" "];
         NSLog(@"Removing period; result is '%@'", correctedString);
         textField.text = correctedString;
         lastRange.location = 0;
         lastRange.length = 0;
     }
}

This is a little buggy (always nixes the period, but occasionally  
moves the cursor to the end of the field even if you were editing the  
middle).

Seth


On Feb 5, 2009, at 10:12 AM, Brad O'Hearne wrote:

>
> Just an FYI, it appears there is no way around this at a UITextField
> level, though it can be changed phone-wide in the Settings > General >
> Keyboard settings (which doesn't help). So this was logged as a new
> feature request to Apple this morning, as was a related issue, which
> is to add callbacks to UITextFieldDelegate which will be called prior
> to auto-correction, auto-capitalization, and auto-period adding after
> two spaces are keyed in, so that the application can accept or veto
> the changes, much in the same way as in the method:
>
> - (BOOL)textField:(UITextField *)textField
> shouldChangeCharactersInRange:(NSRange)range replacementString:
>          (NSString *)string
>
> The bug numbers were 6559455 and 6559471, respectively, in case anyone
> wants to go jump on the pile.
>
> Brad
>
> On Feb 3, 4:52 pm, "Brad O'Hearne" <[EMAIL PROTECTED]> wrote:
>> Ok all,
>>
>> I believe I have a bona fide "stump the iPhone developer" question
>> here. Hopefully it turns out I've overlooked something simple, but as
>> of yet, Apple's iPhone forums and the Phoenix Xcode mailing list
>> haven't yielded a single reply, and Google hasn't yielded anything
>> useful either.
>>
>> I have a UITextField in an iPhone app which has its text input traits
>> set to:
>>
>> Autocapitalize = NONE
>>
>> Autocorrection = NO
>>
>> However, if I try to type two spaces after a letter in the text  
>> field,
>> it automatically adds a "." (period) immediately following the last
>> letter typed. How do I get this turned off? I do not want this period
>> added.
>>
>> Additionally, watching the UITextDelegate callbacks for the
>> UITextField in the debugger reveal something else interesting, that
>> there is never a callback made to:
>>
>> - (BOOL)textField:(UITextField *)textField
>> shouldChangeCharactersInRange:(NSRange)range replacementString:
>> (NSString *)string
>>
>> for the "." that is inserted. There is a callback made for each space
>> keyed in, but that period is changed after-the-fact. Does anyone know
>> how to prevent this behavior? I want just free-form text entry,
>> without any interference and character changing from the API.
>>
>> Knowledge of the "Deep Magic" is appreciated.
>>
>> Cheers,
>>
>> Brad
>>
>> Brad O'Hearne
>> Owner / Developer
>> Big Hill Software
>> ph.480.280.1468
>> fx.888.600.8806
>> [EMAIL PROTECTED]://www.bighillsoftware.com
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Phoenix iPhone Developer Group" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/phoenix-iphone-developer-group?hl=en
-~----------~----~----~----~------~----~------~--~---