Validation on text field: highlighted red border, iPhone - iphone

There are some application in which validated text fields gets highlighted red, when the user enters wrong information into it.
I want to use this validation and highlighting technique on the iPhone too. How can I do that?

Change the border color of a TextField by using QuartzCore
#import <QuartzCore/QuartzCore.h>
[...]
textField.layer.borderWidth = 1.0f;
textField.layer.borderColor = [[UIColor redColor] CGColor];
with rounded corners
textField.layer.cornerRadius = 5;
textField.clipsToBounds = YES;

you can validate the text by setting the UITextField's delegate to your controller then do something like :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range {
[self validateInput]; // Your method to check what the user is writting
return YES;
}
And in your "validateInput", change the background image if the validation fails.

Related

how to set uitextfield bordercolor in iphone application [duplicate]

This question already has answers here:
UITextField border color
(9 answers)
Closed 9 years ago.
how to set uitextfield and UITextView border color programmatically , when we enter or edit in textfield and textview.
I used this code, but doesn't change the border color for the UITextView.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
Don't Forget : #Import <QuartzCore/QuartzCore.h>
Working Code :
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
textField.layer.borderColor=[[UIColor redColor]CGColor];
textField.layer.borderWidth= 1.0f;
return YES;
}
For give Border and Color Of UITextField.
Add #import "QuartzCore/QuartzCore.h" fram work.
textField.layer.borderColor = [UIColor lightGrayColor].CGColor; // set color as you want.
textField.layer.borderWidth = 1.0; // set borderWidth as you want.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
declares if the user is allowed to edit a text field. Change the method to:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
When you edit a text field, there are a lot of methods that seem really similar. You are trying to use-textFieldShouldBeginEditing:. According to the documentation, textFieldShouldBeginEditing, "Asks the delegate if editing should begin in the specified text field." The usage is, "When the user performs an action that would normally initiate an editing session, the text field calls this method first to see if editing should actually proceed. In most circumstances, you would simply return YES from this method to allow editing to proceed." This is not what you wan't to do.
Instead you should use -textFieldDidBeginEditing:. This method, "Tells the delegate that editing began for the specified text field." It, "notifies the delegate that the specified text field just became the first responder. You can use this method to update your delegate’s state information. For example, you might use this method to show overlay views that should be visible while editing."
This means your code should change from:
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
to
-(BOOL)textFieldDidBeginEditing:(UITextField *)textField {
textField.layer.borderColor=[[UIColor cyanColor] CGColor];
}
You can read more about the UITextFieldDelegate methods in the docs at http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

Change UITextField's placeholder text color programmatically

I have a UITextField with a placeholder. When the user wants to submit the form and he/she hasn't typed anything in the textfield, I would like the placeholder's text color become red. Here are my questions:
Would that go against Apple's User interface guidelines? I don't want my app to be rejected because of such small detail.
How I would do it?
I know I can override the method drawPlaceholderInRect: in a subclass of UITextField. But if I did this, the text would be always red and as I wrote before, I would like it to become red depending on a user defined action.
The only solution I can think of is to use a "default" text for my UITextField (the placeholder's text), display it in light grey as long as the user hasn't typed anything and display it in red when I need it. In other words, I would just mock the placeholder's behavior. But of course, this is not very elegant.
Any ideas?
Just look at this:
Digdog Dig - Change UITextField’s placeholder color without subclassing it
[self.MyTextField setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
You can Change the Placeholder textcolor to any color by using the below code. Just try this.
UIColor *color = [UIColor lightTextColor];
YOURTEXTFIELD.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"PlaceHolder Text" attributes:#{NSForegroundColorAttributeName: color}];
like the answer from verklixt but without accessing private api and using UIAppearance:
[[UILabel appearanceWhenContainedIn:[UITextField class], nil] setTextColor:[UIColor darkGrayColor]];
(tested on 5.0 through 7.1)
We can gain access to place holder label using key path,so that we can change color i.e.:
[self.textField setValue:[UIColor **"your color"**]
forKeyPath:#"_placeholderLabel.textColor"];
You can set the placeholder text as a NSAttributedString using this property
NSAttributedString *coloredPlaceholder = [[NSAttributedString alloc] initWithString:#"I am a placeholder string" attributes:#{NSForegroundColorAttributeName: [UIColor redColor]}];
[self.textField setAttributedPlaceholder:coloredPlaceholder];
override
-(void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor darkGrayColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont fontWithName:#"Helvetica-Oblique" size:15.0]];
}
when user does not write any thing in textfield. then put this text as a textfield.text text and change font color.
I had some difficulty implementing color change of placeholder, instead I've found another solution which works perfectly for me.
//On button action change color to red
-(void)btnAction
{
// authentication for missing textfields
if ([textField_firstName.text isEqualToString:#""])
{
textField_firstName.textColor=[UIColor redColor];
textField_firstName.text=#"Enter First Name";
}
}
// in the delegate method of UITextField change the following
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//match the following string with above string and change the string to empty string on textfield click
if ([textField_firstName.text isEqualToString:#"Enter First Name" ])
{
textField_firstName.text=#"";
}
//change back to the text color you use
textField_firstName.textColor=[UIColor blackColor];
}

How can i make a more rounded UITextField? Like the search field in Safari on iPad

HI I'm trying to get a UITextField to look like the google search field available in the safari app on iPad. The purpose of the field will be the same (a search box).
I know i could use a UISearchBar but I would have to use hackish code to get rid of the magnifying glass icon and the background and I don't want that.
I'm attaching an image with the TextField used by apple as their search box. How can I modify an UITextField to look and behave like the search field in this screenshot?
I tried to modified the UITextField's layer roundedCorners property but this doesn't work as expected.
Any help is very much appreciated!
Thank you!
You can set the Corner-Radius on any UIView-Subclass.
Add the QuartzCore.framework to your project
add #import <QuartzCore/QuartzCore.h> in your UIViewController Subclass (where you have access to the UITextfield instance of your choice
within viewDidLoad / viewWillAppear (or any other place where your UITextfield is allready instantiated call [yourTextField.layer setCornerRadius:14.0f];
play around with the cornerRadius value until it looks good
Use the below code.It works for me:
searchField= [[UITextField alloc] initWithFrame:CGRectMake(0,0,150,31)];
searchField.delegate = self;
searchField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchField.textColor = [UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0];
searchField.textAlignment = UITextAlignmentLeft;
searchField.font = [UIFont fontWithName:#"Helvetica" size:15];
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
searchField.clearsOnBeginEditing = NO;
searchField.autocapitalizationType = UITextAutocapitalizationTypeNone;
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.keyboardType = UIKeyboardTypeURL;
searchField.returnKeyType = UIReturnKeySearch;
searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchField.rightViewMode = UITextFieldViewModeUnlessEditing;
searchField.layer.cornerRadius = 17;
searchField.placeholder=#"Google Search";
searchField.borderStyle = UITextBorderStyleRoundedRect;//To change borders to rounded
searchField.layer.borderWidth = 1.0f; //To hide the square corners
searchField.layer.borderColor = [[UIColor grayColor] CGColor]; //assigning the default border color
UIBarButtonItem *searchFieldItem = [[UIBarButtonItem alloc] initWithCustomView:searchField];
Set background property of UITextField to an appropriate image, like this one:
Why don't you try a trick with making separate image of the input field you need and placing it under the real textField control. However, you will have to manipulate the colors of the textField you're placing over in order to make it transparent.
Use a graphic with the rounded edges you want and then position a textview on top.

How to initialize a UITextView programmatically?

How can I initialize a UITextView that is nice and pretty. I have the line:
textView = [[UITextView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
but I'd like to make it look nice and I do not know the functions or the syntax to do so. I'd like it to have word wraping and I'd like the corners to be rounded. Also, how can I make it so that the keyboard goes away if the user presses "Return" on the keyboard. And I'd like to make it say "Done" instead of "Return"
You should check the documentation. This is all in there. Specifically:
you can't set the border, you can only do that for a UITextField
you'll need to set your delegate and implement -textViewDidChange: to check for returns
you can set textView.returnKeyType to UIReturnKeyDone to get the Done button.
You might want to consider using a UITextField, as that object's delegate has a very convenient -textFieldShouldReturn: method, and you can, as I said, set its border.
check out the UItextInputTraits protocol that is linked from the UITextView documentation.
All of these properties can be found there, and others.
EX:
textView.returnKeyType = UIReturnKeyTypeDone;
To make the border look close to a UITextField, you can add the following lines.
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:1.0];
textView.layer.cornerRadius = 3;
textView.clipsToBounds = YES;
To release the keyboard by pressing 'Done'('Return') button, you can override the following protocol (UITextViewDelegate) method.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}

White Text in UITextField = Invisible text in iPhone copy/paste select. Fix?

If I have white text in my UITextField, the selection window (when selecting text) is invisible because the background on the little window is also white.
Any way to fix this?
Sure! The background color of the loupe always matches the backgroundColor property of the text field. (But not the background property.) Example:
textField.textColor = [UIColor whiteColor];
textField.backgroundColor = [UIColor blackColor];
If your text field absolutely requires a transparent background, you'll have to fake it—by using a background image containing the graphics underneath the text field. You may do it manually—by taking a screenshot of your interface and cropping it—or programmatically, like this:
#import <QuartzCore/CALayer.h>
...
// `view` contains the graphics underneath the text field
UIGraphicsBeginImageContext(textField.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint origin = [textField convertPoint:textField.bounds.origin toView:view];
CGContextTranslateCTM(context, -origin.x, -origin.y);
[view.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
textField.background = image;
Since the background is drawn on top of the background color, the text field will appear transparent, and you'll be able to use any background color you want for the loupe.
I'm afraid not. The problem's existed since the earliest days of the iPhone OS—white text appears as white-on-white in the cursor-positioning loupe as well. If this is a serious problem for your app, your only real options are to change the text color or to file a radar feature request with Apple.
Glass takes color from textView.backgroundColor. So I made some dirty hack that works great:
#interface FakeBgTextView : UITextView {
UIColor *_fakeBackgroundColor;
}
- (UIColor *)backgroundColor;
- (void)setFakeBackgroundColor:(UIColor *)color;
#end
#implementation FakeBgTextView
...
- (UIColor *)backgroundColor {
return _fakeBackgroundColor;
}
- (void)setFakeBackgroundColor:(UIColor *)color {
[_fakeBackgroundColor release];
_fakeBackgroundColor = [color retain];
}
...
#end
I know this is a little old, but in iOS5, it appears to be a simulator-only issue. It will render correctly on the device.
One solution that I've employed for this issue is to change the text color to a that will show up in the loupe (but may not look as good in the overall view) while editing and then changing it back to the better display color when finished editing.
It behaves as if you were highlighting the text of the field from a visual standpoint and allows you to use your preferred color for the display of the entered data when not editing.
Set the color to a highlight color that will have enough contrast in the loupe on didBeginEditing, then change it back on didEndEditing.
Just one other possible approach and one I've used in a couple of apps.
eg.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.textColor = [UIColor colorWithRed:116.0/255.0 green:160.0/255.0 blue:246.0/255.0 alpha:1.0];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
textField.textColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0];
}