Ameer
Ameer

Reputation: 725

UITextField Alignment : Placeholder right aligned & Actual text gets left aligned

I have a UITextField set in xib. Text Alignment is set to right. When app is running, I see textfield which shows placeholder with right aligned text. When I start typing it still aligned to right. As soon as I move the cursor from that textfield to some other UI component, typed text gets left aligned.

EDIT : I have resolved it partially in one scenario while editing in textField. But direct text assignment from local data source having same problem.

Editing Text Field Solution

- (void)textFieldDidEndEditing:(UITextField *)textField{
   textField.text = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

Still facing alignment issue if I say,

textField.text = @"my Test";

Applied same logic before assigning text to textField as did in editing mode but still it gets left aligned automatically :(

Actual Output Problem

enter image description here

XIB Setting for UITextField

enter image description here

Upvotes: 0

Views: 2693

Answers (3)

hiren245
hiren245

Reputation: 11

I have tried as same as you performed and it's working fine. I think You have to delete that textfield and put it again with new identity. Then Clean and Rebuild your project.

Make sure You are not giving any values or parameters to textfield programatically.

Upvotes: 0

Mohamad Bachir Sidani
Mohamad Bachir Sidani

Reputation: 2099

You can do the following:
1- set the textAligment to the right
2- add UITextFieldDelegate to your.h
3- yourTextField.delegate = self
4- Add the delegate method 'willBeginEditing' and set the textfield aligment to the left inside it
5- Add the delegate method 'didEndEditing' and set the textfield aligment to the right inside it IF the textfield.text is = @""

Happy Coding!

Upvotes: 0

Ar1f
Ar1f

Reputation: 76

I have been using a UILabel category class for vertical alignment adjustment. In that there was a method +(void)loadand inside that method i was using following line

method_exchangeImplementations(class_getInstanceMethod(self, @selector(textRectForBounds:limitedToNumberOfLines:)), class_getInstanceMethod(self, @selector(verticalAlignTextRectForBounds:limitedToNumberOfLines:)));
method_exchangeImplementations(class_getInstanceMethod(self, @selector(drawTextInRect:)), class_getInstanceMethod(self, @selector(verticalAlignDrawTextInRect:)));

This particular lines of code caused the problem for me. It was overriding the existing default behaviour of the UITexfield. Commenting this worked for me. So something similar would be there in your code also. Hope this will help you :)

Upvotes: 3

Related Questions