1

I am using an iPhone6+ and iOS 8.3.

When, I want to set the UITextField alignment to the right. But when I send it, the input text jump to left.

[tf setBackgroundColor:[UIColor clearColor]];
tf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName:ISMColor(123, 123, 129)}];
tf.delegate = self;
tf.placeholder = placeholder;
[tf setTextAlignment:NSTextAlignmentRight];
[tf setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
perror
  • 7,071
  • 16
  • 58
  • 85
Cx.
  • 59
  • 5

1 Answers1

0

Try implementing this delegate method:

- (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;
}

See more

Community
  • 1
  • 1
SoftDesigner
  • 5,640
  • 3
  • 58
  • 47