How Change placeholder text color - iOS [duplicate] - iphone

This question already has answers here:
iPhone UITextField - Change placeholder text color
(32 answers)
Closed 9 years ago.
How to to change the color of the placeholder text I set in my UITextField controls, to make it black?

using KVC
[yourtextfield setValue:[UIColor colorWithRed:120.0/255.0 green:116.0/255.0 blue:115.0/255.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
you can set your own colour to placeholder

This is a best way to change your placeholder color via KVO...
[txtEmailAddress setValue:[UIColor blackColor] forKeyPath:#"_placeholderLabel.textColor"];
I hope it helps you change placeholder color. Thanks

You can override drawPlaceholderInRect:(CGRect)rect as such to manually render the placeholder text:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}

You will have to subclass the UITextField class and override the following method.
- (void) drawPlaceholderInRect:(CGRect)rect
{
[[UIColor blackColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont italicSystemFontOfSize:17] lineBreakMode:UILineBreakModeCharacterWrap alignment:UITextAlignmentLeft];
}

You can use this to change the placeholder text color.
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}

Make use of the following code. which will help you.
- (void) drawPlaceholderInRect:(CGRect)rect
{
[[UIColor redColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}

Related

SearchBar TintColor no applied correct

I have a problem with the TintColor of my SearchBar. I use the same RGB Color in the navigation bar and on the searchbar. As you can see below, the colors are not the same.
This is the code I use to set the TintColor of the navigationbar:
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:34/255.0f green:113/255.0f blue:179/255.0f alpha:1];
and this is what I use to set the TintColor of the searchBar.
self.mySearchBar.tintColor = [UIColor colorWithRed:34/255.0f green:113/255.0f blue:179/255.0f alpha:1];
So, as you can see, those two lines are pretty much the same. Any ideas on how to get the same color?
Any advice as very appreciated!
Edit:
I develop for iOS 4.0 with XCode 4.3.3 and i tested on a device with iOS 5
I had similar issue and this is what I ended up doing then,
[[UISearchBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:34/255.0f green:113/255.0f blue:179/255.0f alpha:1]]];
[[UIToolbar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:34/255.0f green:113/255.0f blue:179/255.0f alpha:1]] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
But the problem with this approach was that it will not be tinted exactly.
Update:
imageWithColor is category on UIImage.
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

Centering Placeholder Text in Subclass of UITextField

So first, I needed to set the placeholder text color, so I subclassed the UITextfield as shown in several stack overflow posts. This works fine. Here is an example post of this: How do I subclass UITextField and override drawPlaceholderInRect to change Placeholder color.
But, when I try to use searchNameField.textAlignment = UITextAlignmentCenter; It is no longer centering the placeholder. The input text is still centered fine.
So then, assuming that the centering is not working because of the subclassing, what do I have to add to my UITextField subclass in order to have the placeholder text centered?
Thanks
Edit
Here is the code I used to solve this problem. This is placed inside my subclass of UITextField.
- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
CGSize size = [[self placeholder] sizeWithFont:[UIFont fontWithName:#"Arial" size:placeholdTextSize]];
return CGRectMake( (bounds.size.width - size.width)/2 , bounds.origin.y , bounds.size.width , bounds.size.height);
}
Note that placeholdTextSize is a property that I set during initialization.
Here you go buddy
subclassing UITextField will do the work:
// CustomTextField.h
#interface CustomTextField : UITextField {
}
#end
override the methods:
#implementation
- (CGRect)placeholderRectForBounds:(CGRect)bounds {
return CGRectMake(x,y,width,height);//Return your desired x,y position and width,height
}
- (void)drawPlaceholderInRect:(CGRect)rect {
//draw place holder.
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:12]];
}
#end
i think this will work (tested)
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor redColor] setFill];
[self.placeholder drawInRect:rect
withFont:self.font
lineBreakMode:UILineBreakModeTailTruncation
alignment:self.textAlignment];
}
https://github.com/mschulkind/cordova-true-native-ios/blob/master/Classes/UITextFieldPlugin.m
You can do this in UITextfield subclass
- (void)drawPlaceholderInRect:(CGRect)rect {
[[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont boldSystemFontOfSize:16.0] lineBreakMode:UILineBreakModeTailTruncation alignment:NSTextAlignmentCenter];
}

how to set different colors for UIBezeirPath by selecting color options

I am working with UIBezeirPath, and in my drawRect method, I have taken a hardcoded color to setStroke for my path(line) in the below way
- (void)drawRect:(CGRect)rect
{
[[UIColor redColor] setFill];
for (UIBezierPath *_path in pathArray)
{
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
}
this works fine and set red color for the path(line) I draw. Now when I select diffrent color from color options, suppose I select blue color, now when I start to draw the path, the color is blue, but the previous drawn red line also changes to blue.and that is the whole issue
below is my code how I am setting different colors
- (void)drawRect:(CGRect)rect
{
if(changecolor)
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
strokeColor = appDelegate.color;
NSLog(#"%#",strokeColor);
SEL blackSel = NSSelectorFromString(strokeColor);
UIColor* tColor = nil;
if ([UIColor respondsToSelector: blackSel])
tColor = [UIColor performSelector:blackSel];
[tColor setStroke];
[tColor setFill];
}
else
{
[[UIColor redColor] setStroke];
[[UIColor redColor] setFill];
for (UIBezierPath *_path in pathArray)
{
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
}
}
}
Whether I am doing this right way, or am I missing something. please help me out
Regards
Ranjit
Just maintain current color in one array and bezierpaths in another array. Then do the drawing like following.
int q=0;
for (UIBezierPath *_path in pathArray)
{
UIColor *_color = [colorArray objectAtIndex:q];
[_color setStroke];
[_path strokeWithBlendMode:kCGBlendModeNormal alpha:1.0];
q++;
}

UITextField setting backgroundcolor hides shadow

So I'm customizing my UITextField.layer with a shadow:
[userNameField.layer setBorderColor: [[UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1.0] CGColor]];
[userNameField.layer setBorderWidth: 1.0];
[userNameField.layer setCornerRadius:6.0];
[userNameField.layer setShadowOpacity:0.7];
[userNameField.layer setShadowColor:[[UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1.0] CGColor]];
[userNameField.layer setShadowOffset:CGSizeMake(0.5, 0.5)];
This works like a charm but the background is transparent.
Now when I set the background color:
[userNameField.layer setBackgroundColor:[[UIColor whiteColor] CGColor]];
The shadow is overwritten (hidden by the background color).
Does anyone know how to set both the background color AND the shadow on a text field?
You just need to explicitly set the masksToBounds property of the UITextField layer to NO like so:
myTextField.layer.masksToBounds = NO;
try this
textField.borderStyle = UITextBorderStyleRoundedRect;
You can set margin:
myTextField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 7, 7)];
Try this

Placeholder text not centered for UITextField created programmatically

I am creating a UITextField programmatically and placing it inside a UIView. The font size is set to 15.0f (system font). But the placeholder that appears is not centered in the text view. Any one know how to resolve this?
I found some references on SO for blurred text etc and tried setting the frame of the textfield with integer values, but it doesn't make a difference.
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(5.0f, 6.0f, 278.0f, 32.0f)];
[txtField setPlaceholder:#"My placeholder"];
[txtField setFont:[UIFont systemFontOfSize:15.0]];
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setAutocorrectionType:UITextAutocorrectionTypeNo];
[txtField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[txtField setKeyboardType:UIKeyboardTypeEmailAddress];
[txtField setReturnKeyType:UIReturnKeyDone];
[txtField setClearButtonMode:UITextFieldViewModeWhileEditing];
Thank you for any help
Note: I mean that the text is not centered vertically in the textfield (it is a bit towards the top). So setting the text alignment is not the solution.
Adding an image of the issue for clarification - as seen in the image, the placeholder text is more towards the top and not in the center vertically.
You need to add:
txtField.textAlignment = NSTextAlignmentCenter; // Pre-iOS6 SDK: UITextAlignmentCenter
Keep in mind, this adjusts both the alignment of the placeholder text as well as the text the user will enter in.
How to center vertically
Since the original question was updated to request how to vertically align the placeholder text and that answer is buried in the comments, here is how to do that:
txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
This does not work as expected on iOS7. On iOS7 you will have to override TextField class and
- (void) drawPlaceholderInRect:(CGRect)rect
method.
Like this:
- (void) drawPlaceholderInRect:(CGRect)rect
{
[[UIColor blueColor] setFill];
CGRect placeholderRect = CGRectMake(rect.origin.x, (rect.size.height- self.font.pointSize)/2, rect.size.width, self.font.pointSize);
[[self placeholder] drawInRect:placeholderRect withFont:self.font lineBreakMode:NSLineBreakByWordWrapping alignment:self.textAlignment];
}
Works for both iOS7 and earlier versions.
I was using just the color, but we need to set the font as well.
This one worked for me:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:
#{
NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName: [UIFont systemFontOfSize:12]
}];
Changing the minimum line height using NSParagraphStyle was the only solution that worked for me:
NSMutableParagraphStyle *style = [self.addressBar.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy];
style.minimumLineHeight = self.addressBar.font.lineHeight - (self.addressBar.font.lineHeight - placeholderFont.lineHeight) / 2.0;
self.addressBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#"Placeholder text" attributes:#{
NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f],
NSFontAttributeName : placeholderFont,
NSParagraphStyleAttributeName : style
}];