How do I program text into textfield? - ios

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"

Related

textField:shouldChangeCharactersInRange:replacementString: isn't called when inputing in a UITextField

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?

UITextField action - change SKLabelNode

I am using Objective-C to make an iOS SpriteKit "GameScene" file.
I have an SKLabelNode and a UITextField to input a player's name.
How can I make whatever the UITextField value equals to change the SKLabelNode to say "Thanks," + UITextField value?
(my code) :
#import "GameScene.h"
#implementation GameScene
-(void)didMoveToView:(SKView *)view {
// Use Objective-C to declare all SpriteKit Objects
// inside the {parameters} of "didMoveToView" method:
// SKLabelNodes (SpriteKit Object) display "text".
// Objective-C object named *winner = SKLabelNode (SpriteKit Object)
SKLabelNode *winner = [SKLabelNode labelNodeWithFontNamed:#"Tahoma"];
winner.text = #"You Win!";
winner.fontSize = 65;
winner.fontColor = [SKColor blueColor];
winner.position = CGPointMake(500,500);
[self addChild:winner];
// UITextFields (SpriteKit Object) allow "text" input from users.
// Objective-C object named *textField = UITextField (SpriteKit Object)
UITextField *textField = [[UITextField alloc]
initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textField.center = self.view.center;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.textColor = [UIColor blackColor];
textField.font = [UIFont systemFontOfSize:17.0];
textField.placeholder = #"Enter your name here";
textField.backgroundColor = [UIColor whiteColor];
textField.autocorrectionType = UITextAutocorrectionTypeYes;
textField.keyboardType = UIKeyboardTypeDefault;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.view addSubview:textField];
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
#end
You need to use a delegate method. Specifically
- (void)textFieldDidEndEditing:(UITextField *)textField;
to do this when you create your textFeild add this line of code
[textfield setDelegate:self]
also to add in that you fallow the delegate in you .h file
#interface ViewController : UIViewController <UITextFieldDelegate>
then when that code gets called you would say inside the method
SKLabelNode = [NSString stringWithFormat:#"Thanks %#",textField.text]

Create UITextField on UIView but can not get text input

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.

How do I centre UITextField cursor after user input consistently

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

change font color of empty uitextfield

I have a UITextfield and when the text field is equal to "test", I want to clear out the text field and change the font color to green. The code below almost accomplishes it.
- (void)editingDidBegin:(id)sender {
UITextField *txtfld = (UITextField*)sender;
if ([txtfld.text isEqualToString:(#"test")]) {
txtfld.text = #" ";
[txtfld setFont:regularFont];
[txtfld setTextColor:[UIColor greenColor]];
}
}
The only problem is I want to change:
txtfld.text = #" ";
to:
txtfld.text = #"";
When I do that, the font color remains the original black color. If I leave it as #" " then the font changes to green. Any ideas? Thanks in advance.
Scenario:
(1) user enters nothing in the text field and clicks the submit button - works
(2) the submit button updates the text field with the word "test" and makes it red - works (3) when the user goes back to the text field I want the word test deleted and when the user types for all the text to green. - Doesn't work
Note: I placed my code in the event "Editing Did Begin" as i figured when the user goes to update the field it should clear the text and allow them to type in green font color.
It is interesting that when you set text to #"" or nil, and also set textColor in UIControlEventEditingDidBegin event handler, the textColor is not set what so ever. So the silver lining is to set textColor at a later time, also see the comments in code:
- (IBAction)textEditingDidBegin:(id)sender
{
UITextField *field = (UITextField *)sender;
if ([field.text isEqualToString:#"test"]) {
[field setText:#""];
[self performSelector:#selector(setColor:) withObject:sender afterDelay:0.01];
[field setFont:[UIFont fontWithName:#"Georgia" size:15]];
}
}
- (void)setColor:(id)sender
{
// See this line of code closely, do NOT use sender, use textField property in your view controller class
[self.textField setTextColor:[UIColor greenColor]];
}
I just tested your code. It should work. You are reverting the color elsewhere.
You can test it in a new single view project with this code:
#import "ViewController.h"
#interface ViewController () <UITextFieldDelegate>
#property (nonatomic, weak) IBOutlet UITextField *textField;
-(IBAction)checkColor:(UIButton *)sender;
#end
#implementation ViewController
-(void)checkColor:(UIButton *)sender {
_textField.text = #"check color";
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if ([textField.text isEqualToString:#"test"]) {
textField.text = #"";
textField.textColor = [UIColor greenColor];
}
else {
textField.textColor = [UIColor darkTextColor];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
#end

Resources