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

时间:2016-06-20 10:53:18

标签: ios iphone uitextfield

使用不同的字体和不同的文字颜色放置持有人字符串。这就像我的占位符文本的字符串。我的密码占位符应为​​灰色,(最少6个字符)应为浅灰色,字体较小,如下图所示。怎么做到这一点?

enter image description here

5 个答案:

答案 0 :(得分:1)

在swift中使用哪个是相同的逻辑。我们将在目标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 ;

答案 1 :(得分:0)

您可以使用范围属性字符串来实现此目的。这是一个示例代码......

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;

此处10是输入名称的范围密码范围为0.8。

输出textField是 -

enter image description here

希望这会对你有帮助..

答案 2 :(得分:0)

{{1}}

这是您需要的输出

enter image description here

答案 3 :(得分:0)

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;

答案 4 :(得分:-1)

请参阅此链接

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使用此

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