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

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
XIB Setting for UITextField

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 :)

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!

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.

Related

Adding UITextField() to UI programmatically, cannot tap on field to get keyboard

I've been doing iOS development for a long time and I've never run into this before. The UITextField appears in the UI, it is correctly sized and placed by the constraints I've given it, but when I tap/click on it and doesn't respond. The cursor doesn't appear in the field and the keyboard doesn't show up.
let textfield = UITextField()
textfield.translatesAutoresizingMaskIntoConstraints = false
self.contentview.addSubview(textfield)
textfield.keyboardType = .numberPad
textfield.borderStyle = .roundedRect
textfield.text = "stuff"
// and then I set up constraints which are working as expected
I've double checked my simulator to make sure it isn't a soft keyboard issue.
I don't need any specialized UITextFieldDelegate behavior, I just need to standard behavior that tapping will cause it to become first responder, set the cursor there, and open the keyboard. When I add a UITextField via a storyboard, I don't need to set a delegate in order to get this behavior, so I can't imagine I would need to create specialized delegate code in order to get this basic behavior.
Just to test if something else was wrong, I've tried programmatically forcing the textfield to become first responder and that works.
I've tried forcing this to be true, just in case:
textfield.isUserInteractionEnabled = true
but that didn't change anything.
Any ideas? Why isn't my programmatically created UITextField accepting taps?
EDIT: Got an excellent suggestion to check out the Hierarchy Inspector to see if anything else is on top of it. It looks like I don't have anything on top of it: Here's a screenshot of the sim and of the hierarchy inspector turned a bit to the side so you can see the layers.
There isn't anything in front of the UITextField. (The UITextField has like 4 internal layers, but nothing is in front of them.)
As we have discussed in the comments under the OP, it's always a good idea to check the view hierarchy in the View Hierarchy Inspector to see if the frame is laid out properly and also if perhaps some other view isn't covering the other view which should become the first responder.
The view hierarchy inspector can be found here once the app is running on a simulator or a device.

Clear the attributed text of textfield from the IB clear button

I am not able to clear the attributed text of UITextField when the Interface Button clear button is triggered.
I have tried _textfield.attributedText = nil; but it doesn't work. What happens next is that the UI freezes and I am not able to interact with the UI anymore.
Moreover, the attributedText is not removed from the UI.
Note:
I can't use text property of textfield because I have an icon and a string inside my attributedText. Hence, I must use this.
Any help is much appreciated.
Ronak
Why don't you try setting an empty attributed string:
let emptyAttributedString = NSMutableAttributedString()
textView.attributedText = emptyAttributedString
Perhaps you could place this within the IB Action hooked to your clear button?

Trying to implemen custom number pad for UITextField in iOS

I am working on an app where I have a UITableView that has a UITextField in each cell. The textfield is attached to each row via a custom cell. Because there is no number only keypad for the iPad, I have to implement my own (I don't have a choice here). I have created custom buttons for this keyboard inside MainStoryboard, attached these buttons to a view that serves as a custom inputView. These buttons all call the same method which should enter the value of the label on the button, into the textfield. Unfortunately, this is not working. I am implementing the UITextFieldDelegate method:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(#"how about here?");
return YES;
}
which unfortunately does not get called when I place the cursor in any of the textfields, and not when I press any of the keys on the keypad, since I don't see any output on my xcode screen.
I attach the custom keypad to the inputView attribute of each textfield inside my "cellForRowAtIndexPath" method as follows:
_cell.textField.inputView = _inputView;
Can anyone see what I'm doing wrong? I figure implementing the UITextFieldDelegate would be the way to go to accomplish this (which to me appears to be very straight forward), but unfortunately I'm struggling with this. I have also included a screenshot if it helps.
Thanks in advance to all who reply.
Make
textField.delegate = self
or
_cell.textField.delegate = self;
Hope this works !!!

on clicking a text view, always positioning cursor at the beginning

I have a text view. I have not implemented the UITextView Delegate Protocol in my .h, but in .m file I am overriding the delegate methods (shouldBeginEditing and endEditing) and the methods are getting called. Now, what I want to do is whenever user clicks on text view, I always want to position the cursor at the beginning (no matter where he clicks on the text view). I have tried using this lines in textview's BeginEditing method, but it was of no use.
[textView setSelectedRange:NSMakeRange(0, 0)];
I always want to allow user to enter new text at the beginning (don't want the text at the end of already entered text view's text). Someone pls tell me how to get rid of this problem?
PS: I working on ios , xcode 4.2....
UITextView*textView;
[textView setContentOffset:CGPointMake(0, 0) animated:YES];

How to make a new line in UITextView in nib/storyboard?

I am trying to make a new line in Xcode 4.2 UITextView, and when I do alt+return, it goes to the next line, but does not show up when built and ran.
This works, I'm unsure what you are doing differently.
Drag out a UITextView.
Double-click it to edit text, place cursor where you want the paragraph break.
Option-Enter a couple of times to create a blank line & paragraph.
The paragraph shows at runtime. There's also a Text attribute that can be edited in the Attributes inspector.
ALT + Enter makes a new line in storyboard
In the attributes inspector, there is property 'Lines' to set how many lines of text.
Change it to 2.
This is a workaround, but it works everytime and also for UILabels which is more interesting:
Just open a text editor (e.g. textEdit or XCode itself), write the text with the new lines as desired, and copy paste into the Label text property in Interface builder. It shows in both, interface builder and runtime.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//Note: the \n created a new line.
//This piece of code assumes there IS ALREADY text in the theTextView
self.theTextView.text = [[NSString alloc] initWithFormat:#"%# \n\n a line appart",self.theTextView.text];
}
Assuming you created the "theTextView" in the (.h) file and synthesized it in the (.m) file and made the appropriate connection.
I can give a more clear answer if you tell al wen exactly the new line must be made... Programmatically of when the user presses buttons?
Drag a view (resize to a line) and add it a background color.
I'm not sure if there's another way.

Resources