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];
}
Related
Is there any easy way to set a text field's hint to italic, while keeping the actual text non-italicized? The image below shows what I'm trying to accomplish, but doesn't actually work, as text typed into the first text field is still italicized.
If there's no easy way to do this, I'm thinking that I'll have to implement methods on each of the text fields to check the length of the text, and if zero apply the italic font. This would work, right? Is there a better way?
Try this:
UIFont* italicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[yourTextField setValue:italicFont forKeyPath:#"_placeholderLabel.font"];
EDIT:
Ok, here is another (legal) way to do it:
1.Subclass UITextField and override drawPlaceholderInRect::
TextFieldSubclass.h:
#interface TextFieldSubclass : UITextField
#end
TextFieldSubclass.m:
- (void) drawPlaceholderInRect:(CGRect)rect
{
[[UIColor lightGrayColor] setFill];
UIFont* italicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[[self placeholder] drawInRect:rect withFont:italicFont];
}
2.Change your UITextField class and set it to TextFieldSubclass:
And that's it. Enjoy.
I have tried the two options everybody answers in this forum but nothing works for me... I have tried to override:
- (void)drawPlaceholderInRect:(CGRect)rect
and also:
[self.myTextField setValue:[UIColor redColor] forKeyPath:#"_placeholderLabel.textColor"];
but nothing happens... Still the same gray color... Maybe I have to inherit something, I don't know... Any suggestion?
the simplest way to do this:
UILabel *placeholderAppearance;
if (#available(iOS 9, *)) {
placeholderAppearance = [UILabel appearanceWhenContainedInInstancesOfClasses:#[[UITextField class]]];
} else {
placeholderAppearance = [UILabel appearanceWhenContainedIn:[UITextField class], nil];
}
placeholderAppearance.textColor = [UIColor redColor];
[EDITED]
After using this I got to know another bug, the above did the job but it also reset the color of all other labels in my view to Default Black. Then to overcome this, I had to sub class the UILabel class and use my class in all other labels in my view.
See drawPlaceholderInRect: of UITextField. You need to subclass UITextField and override this method for configuring graphics context with desired text color and call super implementation.
By the time this method is called, the current graphics context is
already configured with the default environment and text color for
drawing. In your overridden method, you can configure the current
context further and then invoke super to do the actual drawing or do
the drawing yourself. If you do render the text yourself, you should
not invoke super.
From iOS6 you can use the attributed placeholder:
textfield.attributedPlaceholder = [[NSAttributedString alloc] initWithString:textfield.placeholder attributes:#{NSForegroundColorAttributeName: [UIColor redColor]}];
It's seen in UITextView and UITextField - the flashing blue line which shows you where anything you type will be typed. I've seen some apps change the color of this and wasn't sure how to do that. How do I change it's color?
In IOS 7 you can simple set tintColor, it change color for all active elements in view and subviews, includes cursor color:
_textField.tintColor = [UIColor redColor];
[[textField textInputTraits] setValue:[UIColor redColor] forKey:#"insertionPointColor"];
Try this:
[[textField valueForKey:#"textInputTraits"] setValue:CUSTOM_COLOR forKey:#"insertionPointColor"];
Despite it seems to be undocumented, but it works. Frankly, you don't use any
private methods here - only Key-Value Coding which is legal.
P.S. Yesterday my new app appeared at AppStore without any problems with this approach. And it is not the first case when I use KVC in changing some read-only properties (like navigatonBar) or private ivars.
There is no documented way to change the pointer's color. Hovewer, there is one undocumented textField's property, namely textInputTraits, which is an UITextInputTraits instance.
You can try this code to get this working, but your app may be rejected in review process.
[[textField textInputTraits] setValue:[UIColor redColor] forKey:#"insertionPointColor"];
Taken from https://stackoverflow.com/a/4273440/736649
For iOS 10 SDK , only the indicator, and Objective-C (someday will be missed) my working solution is :
for (UIView *subView in self.sBar.subviews){
for (UIView *ndLevelSubView in subView.subviews){
if ([ndLevelSubView isKindOfClass:[UITextField class]])
{
UITextField * sbTextField = (UITextField *)ndLevelSubView;
[[sbTextField valueForKey:#"textInputTraits"] setValue:[UIColor n2pMainPurpleColor] forKey:#"insertionPointColor"];
break;
}
}
}
Change the tint color, (iOS 7)
or do it programmatically
myTextField.tintColor = [UIColor blackColor];
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.
Is there a way to draw colored text using the UIKit addition for NSString on the iPhone?
Brad's solution adds quite a bit of code if you do not have a context handy (for example if you are doing this in a drawRect: method).
The way to do it without CG is
[[UIColor redColor] set];
or whatever colour you want.
If by the NSString UIKit additions, you mean the category methods drawAtPoint: and drawInRect:, then all you need to do to change their drawn color is place
CGContextSetFillColorWithColor(context, textColor);
before you call drawAtPoint: or drawInRect:. The context's fill color is used as the text's color when it is drawn manually.
The NSString itself has no knowledge of how it will be displayed; it's simply data. The display is handled in whatever view is presenting the text to the user. It can be a UILabel, a UITextfield, etc... These classes typically store their text in either a UILabel or a text property. The text color can usually be set by with:
aLabel = [[UILabel alloc] init];
aLabel.text = #"My String";
aLabel.textColor = [UIColor blackColor];
On the desktop you can use NSAttributedString, but this isn't available on the iPhone. If you're just trying to put colored text on the screen, the easiest way is to just use a UILabel. You can manipulate the text, font and color like this:
myLabel.text = #"Your text";
myLabel.font = [UIFont boldSystemFontOfSize:16];
myLabel.textColor = [UIColor redColor];