答案 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
是 -
希望这会对你有帮助..
答案 2 :(得分:0)
答案 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];
}