Place holder string with different font and different text color. It is like attributed string for my place holder text. My Password placeholder should be gray in color and (Min 6 characters) should be light gray in color with small font size, like show in the image below. How to achieve this?
Asked
Active
Viewed 900 times
1
-
Try searching for attributedPlaceholder for textfield.or in general attributed strings and their functions. – Sanman Jun 20 '16 at 10:58
-
plz see this link http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color – balkaran singh Jun 20 '16 at 11:04
5 Answers
1
Which is use in swift this are the same logic. we will do it in the objective c
var myMutableStringTitle = NSMutableAttributedString()
let Name = "Enter Title" // PlaceHolderText
myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 10.0)!]) // Font
myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:5)) // Color
myMutableStringTitle.addAttribute(NSFontAttributeName, value: UIFont(name: "Georgia", size: 36.0)!, range: NSRange(location: 0, length: 5))
textfield.attributedPlaceholder = myMutableStringTitle ;

HariKrishnan.P
- 1,204
- 13
- 23

Priya Bijawat
- 66
- 3
-
Please edit your answer and explain about your piece of code. So, this will be helpful for all the users. – Dray Jun 20 '16 at 11:35
0
You can achieve this using range and attributed string. Here is a sample code...
NSMutableAttributedString *attString =
[[NSMutableAttributedString alloc]
initWithString:@"Enter Name (minimum 6 char)"];
[attString addAttribute: NSForegroundColorAttributeName
value: [UIColor redColor]
range: NSMakeRange(0,10)];
[attString addAttribute: NSFontAttributeName
value: [UIFont boldSystemFontOfSize:16.0f]
range: NSMakeRange(0,10)];
[attString addAttribute: NSForegroundColorAttributeName
value: [UIColor grayColor]
range: NSMakeRange(10 + 1,@"Enter Name (minimum 6 char)".length - 10 -1)];
[attString addAttribute: NSFontAttributeName
value: [UIFont systemFontOfSize:12.0f]
range: NSMakeRange(10 + 1,@"Enter Name (minimum 6 char)".length - 10 -1)];
textField.attributedPlaceholder = attString;
here 10 is range of enter name in your case Password range is 0,8.
Output textField
is-
Hope this will help you..

Rohit Khandelwal
- 1,778
- 15
- 23
0
I hope this is helped to u
NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Enter Name (minimum 6 char)"];
[hogan addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:12.0]
range:NSMakeRange(11, 16)];
NSRange rangeOfUsername = [[hogan string] rangeOfString:@"Enter Name"];
if (rangeOfUsername.location != NSNotFound) {
[hogan addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:rangeOfUsername];
}
textX.attributedPlaceholder = hogan;
This the output you need

HariKrishnan.P
- 1,204
- 13
- 23
0
Swift code.
let aString1 = NSMutableAttributedString(string: "Password", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(18.0),NSForegroundColorAttributeName:UIColor.blackColor()])
let aString2 = NSAttributedString(string: " (Min 6 chars)", attributes: [NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 10.0)!,NSForegroundColorAttributeName:UIColor.grayColor()])
aString1.appendAttributedString(aString2)
textFeild!.attributedPlaceholder = aString1;

Amit Kalghatgi
- 357
- 3
- 16
-1
see this link
iPhone UITextField - Change placeholder text color
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
for ios 7.0 use this
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary =
[NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
[NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil];
[[self placeholder] drawInRect:rect withAttributes:attrsDictionary];
}

Community
- 1
- 1

balkaran singh
- 2,754
- 1
- 17
- 32
-
How is this function useful. It changes the color of whole place Holder string. – Priyansh Jun 20 '16 at 11:12
-
you need to create a sub class of UITextField . then use this custom class to create the uitext field – balkaran singh Jun 20 '16 at 11:14