如何使用Swift 2.0更改UITextfield的占位符颜色和字体大小?

22

如何在SWIFT 2.0中更改UITextfieldplaceholderfontsize


https://dev59.com/vl8e5IYBdhLWcg3wHXaJ#26076202 - pkc456
2
可能是使用Swift更改占位符文本颜色的重复问题。 - Eric Aya
11个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
85

#1. 程序化地设置文本框的占位符颜色

    var myMutableStringTitle = NSMutableAttributedString()
    let Name  = "Enter Title" // PlaceHolderText

    myMutableStringTitle = NSMutableAttributedString(string:Name, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 20.0)!]) // Font
    myMutableStringTitle.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range:NSRange(location:0,length:Name.characters.count))    // Color
    txtTitle.attributedPlaceholder = myMutableStringTitle

或者

txtTitle.attributedPlaceholder = NSAttributedString(string:"Enter Title", attributes: [NSForegroundColorAttributeName: yellowColor])

注意: Name 是你的文本框的占位符。

占位符文本框:

图片描述

-------------------------------- 或 -------------------------------------

#2. 在运行时属性中设置占位符文本颜色

  1. 设置文本框的占位符为输入标题

    图片描述

  2. 点击文本框属性的身份检查器。

    图片描述

  3. 用户定义运行时属性,添加颜色属性

    关键路径: _placeholderLabel.textColor

    类型: Color

    值: 您的颜色或RGB值

    图片描述

    占位符文本框:

    图片描述


1
第二种解决方案对我来说最好,我想将所有字段分组到一个“IBOutletCollection”中,但是我不确定如何定义每个UITextfields占位符字符串。这种方式非常适用于Swift 3。 - Karl Taylor
1
5年在iOS上工作,竟然不知道第二种解决方案!太完美了!谢谢 :) - Lapinou
1
我正在阅读关于第二种解决方案的内容,显然在生产中可能会有一些问题,因为苹果可能会拒绝它。 - Patricio Vargas
7
现在(对于Swift 5,13.4.1),应该使用placeholderLabel.textColor而不是_placeholderLabel.textColor。 - Zaporozhchenko Oleksandr

17

已更新至Swift 3

如果您想在Swift 3中更改UITextField的占位符颜色,请使用以下代码:

let yourTextFieldName = UITextField(frame: CGRect(x: 0, y: 0, width: 180, height: 21))
yourTextFieldName.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])

14

更新至Swift 5

在Swift 5.0中,请使用NSAttributedString.Key.foregroundColor,而不是NSForegroundColorAttributeName

因此,请像这样操作

textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])

10

对于Swift 4而言,不再使用

NSForegroundColorAttributeName

而是使用

NSAttributedStringKey.foregroundColor


这不是问题的答案 - Zaporozhchenko Oleksandr

6
您可以尝试使用这个样例代码。
let  textFld = UITextField();
textFld.frame = CGRectMake(0,0, 200, 30)
textFld.center = self.view.center;
textFld.attributedPlaceholder = NSAttributedString(string:"Test Data for place holder", attributes:[NSForegroundColorAttributeName: UIColor.blueColor(),NSFontAttributeName :UIFont(name: "Arial", size: 10)!])
self.view.addSubview(textFld)

3

文本框的占位符 Objective C

NSString* str = @"Placeholder text...";                                    
NSRange range1 = [str rangeOfString:@"Placeholder text..."];


NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:str];
[attributedText setAttributes:@{
                                NSFontAttributeName:[UIFont fontWithName:customFont_NotoSans_Regular size:13.0]
                                }
                        range:range1];

[attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range1];

txtFld.font = [UIFont fontWithName:customFont_NotoSans_Regular size:13.0];
txtFld.keyboardType = UIKeyboardTypeDefault;
txtFld.attributedPlaceholder = attributedText;

3

使用UITextField的子类很容易实现。

添加placeholderColor属性以轻松设置颜色,然后观察.placeholder的变化以将颜色应用于它(使用.attributedPlaceholder属性)。

var placeholderColor: UIColor = .lightGray

override var placeholder: String? {
    didSet {
        let attributes = [ NSAttributedString.Key.foregroundColor: placeholderColor ]
        attributedPlaceholder = NSAttributedString(string: placeholder ?? "", attributes: attributes)
    }
}

您需要通过编程设置占位文本,才能应用颜色。


这正是我一直在寻找的!子类化和覆盖默认行为。谢谢! :) - Vitya Shurapov

0

设置文本框占位符

let leftBarButtonItem = UIBarButtonItem.init(image: UIImage(named:"ic_nav-bar_back.png"), landscapeImagePhone: nil, style: .plain, target: viewController, action: #selector(viewController.buttonClick(_:)))
        leftBarButtonItem.imageInsets = UIEdgeInsets(top: 0, left: -15, bottom: 0, right: 0)
        leftBarButtonItem.tintColor = UIColor(hex: 0xED6E19)
        viewController.navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)

0

通过选择文本字段打开您的身份检查器,然后按下 + 按钮,在关键路径中输入“placeholderLabel.textColor”。 将类型设置为“Color”,在值中选择所需的RGB颜色。


0

Swift 5

textfiled.attributedPlaceholder = NSAttributedString(string:NSLocalizedString("Input Group Name", comment: "Input Group Name"), attributes: [NSAttributedString.Key.foregroundColor: yourColor.withAlphaComponent(0.5)])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,