简体   繁体   中英

UITextField placeholder - String with different colors and Font?

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?

在此输入图像描述

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 ;

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..

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

在此输入图像描述

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM