简体   繁体   English

UITextField占位符 - 具有不同颜色和字体的字符串?

[英]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. 我的密码占位符应为​​灰色, (最少6个字符)应为浅灰色,字体较小,如下图所示。 How to achieve this? 怎么做到这一点?

在此输入图像描述

Which is use in swift this are the same logic. 在swift中使用哪个是相同的逻辑。 we will do it in the objective c 我们将在目标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. 这里10是你输入名称的范围密码范围是0.8。

Output textField is- 输出textField是 -

在此输入图像描述

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. SWIFT代码。

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 iPhone UITextField - 更改占位符文本颜色

- (void) drawPlaceholderInRect:(CGRect)rect {
    [[UIColor blueColor] setFill];
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}

for ios 7.0 use this 对于ios 7.0使用此功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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