In my iPad app, I noticed different behavior between iOS 6 and iOS 7 with UITextFields.
I create the UITextField as follows:
UIButton *theButton = (U
I've came up with a solution that subclasses the UITextField class and performs the swap, without the need of copying and pasting code everywhere. This also avoids using method sizzle to fix this.
@implementation CustomTextField
-(id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if( self ) {
[self addSpaceFixActions];
}
return self;
}
- (void)addSpaceFixActions {
[self addTarget:self action:@selector(replaceNormalSpaces) forControlEvents:UIControlEventEditingChanged];
[self addTarget:self action:@selector(replaceBlankSpaces) forControlEvents:UIControlEventEditingDidEnd];
}
//replace normal spaces with non-breaking spaces.
- (void)replaceNormalSpaces {
if (self.textAlignment == NSTextAlignmentRight) {
UITextRange *textRange = self.selectedTextRange;
self.text = [self.text stringByReplacingOccurrencesOfString:@" " withString:@"\u00a0"];
[self setSelectedTextRange:textRange];
}
}
//replace non-breaking spaces with normal spaces.
- (void)replaceBlankSpaces {
self.text = [self.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];
}