I have a UITextField that I want to centre all content (text, cursor) at all times. Is this possible in iOS 7? My current initialisation of the view is shown below.
self.textField = [[UITextField alloc] init];
self.textField.delegate = self;
[self.textField setTextAlignment:NSTextAlignmentCenter];
self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
self.textField.placeholder = NSLocalizedString(#"Enter some text", #"The placeholder text to use for this input field");
My requirement for this is that when I click in the UITextField, the placeholder text should disappear, and show the cursor in the middle of the UITextField.
Currently, this seems to be intermittently positioned either in the middle of the text field or on the left of the text field irrelevant of where I click. Anybody got any suggestions as to how I can solve this or is it a known issue in iOS?
If you're creating you UITextField in you Storyboard you should not initialise and alloc it in code.
You need to use you Textfield delegates to accomplish this..
- (void)viewDidLoad
{
[super viewDidLoad];
self.textField.delegate = self;
[self.textField setTextAlignment:NSTextAlignmentCenter];
self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
[self.textField setTranslatesAutoresizingMaskIntoConstraints:NO];
self.textField.placeholder = #"Enter some text";
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
//this removes your placeholder when textField get tapped
self.textField.placeholder = nil;
//this sets your cursor to the middle
self.textField.text = #" ";
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
self.textField.placeholder = #"The placeholder text to use for this input field";
}
This surely does the trick.. Please accept the answer if it helps.
UPDATE
With code below when user press backspace the cursor will not align the text to the left.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *proposedNewString = [[textField text] stringByReplacingCharactersInRange:range withString:string];
NSLog(#"propose: %#", proposedNewString);
if ([proposedNewString isEqualToString:#""])
{
textField.text = [#" " stringByAppendingString:textField.text];
}
return YES;
}
Related
I have a UITextField and I want it has a maximum text length of 20. So I let self(the current view controller) agree to <UITextFieldDelegate> and set self as the text field's delegate, and use the following method to set a maximum length:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == self.nickNameTextField)
{
if (textField.text.length > 20)
return NO;
}
return YES;
}
However when I insert a breakpoint in this function I found that this function wasn't called when I typed in any words, much less when the text's length exceeds 20. Can somebody tell me why?
You probably haven't linked your textfield with the delegate
Right click on textField which you want to have delegated and drag the textField on the viewcontroller and drop to select the delegate.
I show you steps and screenshot for connecting textField and delegate with XIB or Storyboard.
In storyboard
STEP 1: Click the TextField
STEP 2: Drag the TextField to ViewController
STEP 3: Click "control+mouse" on TextField to ViewController.h and line shows
STEP 4:Now Box will appear and give the TextField Name
STEP 5:Right Click the TextField in ViewController
STEP 6:Hook Up the Delegate to ViewController
If you want to programmatically create the textField
UITextField *txtfldMaxLength = [[UITextField alloc] initWithFrame:CGRectMake(30, 100, 250, 50)];
txtfldMaxLength.borderStyle = UITextBorderStyleRoundedRect;
txtfldMaxLength.textColor = [UIColor blackColor];
txtfldMaxLength.backgroundColor = [UIColor clearColor];
txtfldMaxLength.font = [UIFont systemFontOfSize:17];
txtfldMaxLength.placeholder = #"enter something";
txtfldMaxLength.autocorrectionType = UITextAutocorrectionTypeNo;
txtfldMaxLength.keyboardType = UIKeyboardTypeDefault;
txtfldMaxLength.returnKeyType = UIReturnKeyDone;
txtfldMaxLength.clearButtonMode = UITextFieldViewModeWhileEditing;
txtfldMaxLength.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
txtfldMaxLength.delegate = self;
[self.view addSubview:txtfldMaxLength];
Well I've figured out the problem. I use this grammar to instantiate a UITextField:
self.nickNameTextField.delegate = self;
self.nickNameTextField = ({
UITextField *textField = [UITextField new];
textField.placeholder = [BBTCurrentUserManager sharedCurrentUserManager].currentUser.nickName;
textField.textAlignment = NSTextAlignmentLeft;
textField.backgroundColor = [UIColor lightGrayColor];
textField;
});
Originally I set the textField's delegate outside the braces(like the code above). But when I move self.nickNameTextField.delegate = self;inside the braces then it worked. However I have no idea about why this happened, can someone tell me why?
I am using Xcode 6.3 with Objective-C language. The app that I am building has UITextField and UITextView. I am wanting to have text show up in the fields/views so that the user will have instructions on what to put into the fields/views. Example, "enter your name". How can I do this?
UITextField:
For your UITextField you should define the placeholder for the instructions text.
_textField.placeholder= #"Your Name";
UITextView :
UITextView does not have a default placeholder like UITextField.
But you can make the effect like placeholder by following these steps.
- (void)viewDidLoad {
[super viewDidLoad];
_textView.text = #"Enter Comment...";
_textView.textColor = [UIColor lightGrayColor];
_textView.delegate = self;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:#"Enter Comment..."]) {
textView.text = #"";
textView.textColor = [UIColor blackColor];
}
[textView becomeFirstResponder];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if ([textView.text isEqualToString:#""]) {
textView.text = #"Enter Comment...";
textView.textColor = [UIColor lightGrayColor];
}
[textView resignFirstResponder];
}
You may be looking for the placeholder property.
let t = UITextField()
t.placeholder = "enter your name"
if you wann change the text just call:
textfield.text = #"enter your name"
if you just want to set a placeholder which will disappear on user input use this property :
textfield.placeholder = #"enter your name"
if you want a list instructions , please add /place a UIView in appropriate location and show and hide inside your textFieldDidBeginEditing: delegate method.
Note :- place holders might not be appropriate as it has limit to text and your instructions may be huge.
Place holders for text fields is the way you can have some text shown...
textField.placeholder = "Enter your name"
I am using Xcode 6 and doing a small project on iOS 8, and I need to render some text onto the View.
My method is to create a UITextField on a UIView and as long as people type onit, the app redraw the View:
- (void)viewDidLoad {
[super viewDidLoad];
self.renderTextView.textToRender = #"Please type something…";
UITextField* textField = [[UITextField alloc] initWithFrame:self.renderTextView.frame];
textField.opaque = NO;
[self.renderTextView addSubview:textField];
[textField addTarget:self action:#selector(updateLabelAndRefresh:) forControlEvents:UIControlEventEditingChanged];
}
- (void) updateLabelAndRefresh: (id)sender{
self.renderTextView.textToRender = #"Hello World";
// if text's length > 0 …
if (self.textField.text.length > 0) {
self.renderTextView.textToRender = self.textField.text;
}
[self.renderTextView setNeedsDisplay];
}
But the problem is: no matter how I try, I can not get the actual text I type on my phone. and the console showed me that the text is null. I kept googling it but I stil can not work it out.
Do you guys have any solution? Thank you ;)
In below some error is there, give a look.
- (void)viewDidLoad {
[super viewDidLoad];
//Here textField is local variable
UITextField* textField = [[UITextField alloc] initWithFrame:self.renderTextView.frame];
textField.opaque = NO;
}
- (void) updateLabelAndRefresh: (id)sender{
self.renderTextView.textToRender = #"Hello World";
// if text's length > 0 …
if (self.textField.text.length > 0) { //Then how come u getting self.textField here and using it as global one.
//So you won't get access to that locally defined textField in viewDidLoad method.
self.renderTextView.textToRender = self.textField.text;
}
}
Please check your code and update me about it.
I´m creating some UITextFields programmatically, and when I try to delegate them I receive this warning:
NAVPlanViewController *const __strong' to parameter of incompatible type id<UITextFieldDelegate>
textHeight = [[UITextField alloc] initWithFrame:CGRectMake(0, 120, 160, 40)];
[textHeight setDelegate:self];
and in .c of my class I have added #interface NAVPlanViewController : UIViewController <UITextViewDelegate>.
Also, I need to restrain text field to accept only numbers, I thought to do it with this code but since UITextField wont Delegate I was not able to check it:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
if (!string.length) {
return YES;
}
if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet]
invertedSet]].location != NSNotFound){
//do something;
return NO;
}
In your NAVPlanViewController interface definition, <UITextViewDelegate> should be <UITextFieldDelegate>.
#neilco is right conform to like
#interface YourViewController ()<UITextFieldDelegate>
{
}
#end
And to except only numbers, set the keyboardType to UIKeyboardTypeNumberPad like
UITextField *age = [UITextField autolayoutView];
age.placeholder = kPlaceholderToEnterAge;
age.borderStyle = UITextBorderStyleRoundedRect;
age.clearButtonMode = UITextFieldViewModeWhileEditing;
age.returnKeyType = UIReturnKeyDefault;
age.keyboardType = UIKeyboardTypeNumberPad;
age.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
age.textAlignment = NSTextAlignmentCenter;
I need to use the, Name Phone Keyboard type (UIKeyboardTypeNamePhonePad) but having the number pad appear first when the text field is selected.
I need to input data such as
123 -
657 -
450b -
123a
I always need input a number first, but occasionally either a A / B at the end.
You can try to change keyboard type in function of the size of the string:
Implement delegate UITextFieldDelegate
Then set defaut textfield keyboard;
[textField setKeyboardType:UIKeyboardTypeNumberPad];
[textField setDelegate:self];
Then in the following method, change the keyboard when text size equals 3:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([[textField text] length] > 3)
{
[textField setKeyboardType:UIKeyboardTypeDefault];
[textField reloadInputViews];
}
return YES;
}
Another solution is to add an accessory view button (you can change of keyboard manually):
- (void)viewDidLoad
{
[super viewDidLoad];
textField.keyboardType = UIKeyboardTypeNumberPad;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 50, 50)];
[button addTarget:self action:#selector(changeKeyboard) forControlEvents:UIControlEventTouchDown];
textField.inputAccessoryView = button;
}
-(void)changeKeyboard
{
if([textField keyboardType] == UIKeyboardTypeNumberPad)
{
textField.keyboardType = UIKeyboardAppearanceDefault;
}
else
{
textField.keyboardType = UIKeyboardTypeNumberPad;
}
[textField reloadInputViews];
}
click on the textfield,then go to Hide or show utilities ie View(you will find it at right hand side beside the editor.There you select the third view ie the most right one).
Then you click the attributes inspector.In that you will find a menu called "keyboard" which is set as default,but you can select whichever pad you want.
Hope it works for you.