Right Aligned Uitextfield Spacebar Does Not Advance Cursor in iOS 7

Right aligned UITextField spacebar does not advance cursor in iOS 7

It would be a bit of a hack, but if you really need that to look the iOS6 way, you can replace space with non-breaking space as it's written. It's treated differently. Example code could look like this:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// only when adding on the end of textfield && it's a space
if (range.location == textField.text.length && [string isEqualToString:@" "]) {
// ignore replacement string and add your own
textField.text = [textField.text stringByAppendingString:@"\u00a0"];
return NO;
}
// for all other cases, proceed with replacement
return YES;
}

In case it's not clear, textField:shouldChangeCharactersInRange:replacementString: is a UITextFieldDelegate protocol method, so in your example, the above method would be in the viewcontroller designated by [textField setDelegate:self].

If you want your regular spaces back, you will obviously also need to remember to convert the text back by replacing occurrences of @"\u00a0" with @" " when getting the string out of the textfield.

UITextField Right Alignment iOS 7

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.location == textField.text.length && [string isEqualToString:@" "]) {
// ignore replacement string and add your own
textField.text = [textField.text stringByAppendingString:@"\u00a0"];
return NO;
}
// for all other cases, proceed with replacement
return YES;
}

To remove the code from text

self.txtFirstName.text = [self.txtFirstName.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];

FROM this stackoverflow answer - Right aligned UITextField spacebar does not advance cursor in iOS 7

Space at end of text not visible in right-aligned UITextField

Seems like triazotan has a solution involving replacing non breaking space for space https://stackoverflow.com/a/20129483/1247248

rednaw has some potentially better version, but someone was complaining of weird side effects
https://stackoverflow.com/a/22512184/1247248

I ended up going with the version of this solution suggested by meaning-matters https://stackoverflow.com/a/22211018/1247248 and that worked for me.

EDIT

It looked like a good fix but I found a problem. When you select inside the text and type the cursor jumps to the end of the line. Turns out I should have gone with triazotan's version
https://stackoverflow.com/a/20129483/1247248

Rightalignment UITextfield's cursor go left after enter a space

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// only when adding on the end of textfield && it's a space
if (range.location == textField.text.length && [string isEqualToString:@" "]) {
// ignore replacement string and add your own
textField.text = [textField.text stringByAppendingString:@"\u00a0"];
return NO;
}
// for all other cases, proceed with replacement
return YES;
}

In case it's not clear, textField:shouldChangeCharactersInRange:replacementString: is a UITextFieldDelegate protocol method, so in your example, the above method would be in the viewcontroller designated by [textField setDelegate:self].

If you want your regular spaces back, you will obviously also need to remember to convert the text back by replacing occurrences of @"\u00a0" with @" " when getting the string out of the textfield

For Ref: Right aligned UITextField spacebar does not advance cursor in iOS 7

iOS - UITextField - No cursor when editing text

I think your cursor color is similar to text background color

you can change by this

OBj-C [[UITextField appearance] setTintColor:[UIColor blackColor]];

Swift UITextField.appearance().tintColor = .black



Related Topics



Leave a reply



Submit