22

How to change UITextfield placeholder & fontsize in SWIFT 2.0?

SergioM
  • 1,595
  • 1
  • 15
  • 27
Xcode
  • 455
  • 2
  • 5
  • 16
  • http://stackoverflow.com/a/26076202/988169 – pkc456 Oct 29 '15 at 12:51
  • 2
    Possible duplicate of [Changing Placeholder Text Color with Swift](http://stackoverflow.com/questions/26076054/changing-placeholder-text-color-with-swift) – Eric Aya Oct 29 '15 at 12:55

11 Answers11

85

#1. set Placeholder textfield color Programmatically

    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

OR

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

Note : Name is your placeholder of textField.

PlaceHolder TextFiled :

enter image description here

-------------------------------- OR -------------------------------------

#2. set Placeholder textfield color at runtime attribute

  1. Set textfield placeHolder text Enter Title

    enter image description here

  2. Click on identity inspector of textfield property.

    enter image description here

  3. User Define Runtime Attributes, add color attributes

    Key Path : _placeholderLabel.textColor

    Type : Color

    value : Your Color or RGB value

    enter image description here

    PlaceHolder TextFiled :

    enter image description here

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
  • 1
    The 2nd solution was best for me, I wanted to group all the fields in a `IBOutletCollection` however I was unsure how to define each UITextfields placeholder strings. This way works perfect for Swift 3. – Karl Taylor Jan 30 '17 at 13:57
  • 1
    5 years working on iOS and didn't know the second solution ! Perfect ! Thank you :) – Lapinou Jun 22 '18 at 06:53
  • 1
    I was reading about the second solution and it could have some problem in production apparently because Apple can reject it – Patricio Vargas Sep 23 '18 at 19:14
  • 7
    now (for swift 5, 13.4.1) it should be placeholderLabel.textColor instead of _placeholderLabel.textColor – Zaporozhchenko Oleksandr May 13 '20 at 19:16
17

Updated for Swift 3

If you want to change the UITextField Placeholder color for Swift 3, use the following lines of code:

let yourTextFieldName = UITextField(frame: CGRect(x: 0, y: 0, width: 180, height: 21))
yourTextFieldName.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSForegroundColorAttributeName: UIColor.white])
Pang
  • 9,564
  • 146
  • 81
  • 122
Kiran Jadhav
  • 3,209
  • 26
  • 29
14

Updated for Swift 5

For swift 5.0 use NSAttributedString.Key.foregroundColor instead of NSForegroundColorAttributeName

So, do it like so

textField.attributedPlaceholder = NSAttributedString(string: "placeholder text", attributes: [NSAttributedString.Key.foregroundColor: UIColor.white])
Jin
  • 665
  • 1
  • 7
  • 15
10

For swift 4 instead of

NSForegroundColorAttributeName

use

NSAttributedStringKey.foregroundColor

PowerSurge
  • 591
  • 7
  • 5
6

You can try with this sample code

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)
Jamil
  • 2,977
  • 1
  • 13
  • 23
3

Placeholder for textfield 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;
Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
3

It's easy to do with a subclass of UITextField.

Add placeholderColor property to easily set the color, and then observer changing of .placeholder to apply the color to it (with use of .attributedPlaceholder property)

var placeholderColor: UIColor = .lightGray

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

You do need to set the placeholder text programatically for the color to apply.

Senõr Ganso
  • 1,694
  • 16
  • 23
0

set Textfield placeholder

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)
Srinivasan_iOS
  • 972
  • 10
  • 12
0

A simple solution is override placeholder property in an UITextField extension. It will update color of placeholder whole project. You don't need to update your code in many places.

extension UITextField {
  var placeholder: String? {
      get {
          attributedPlaceholder?.string
      }

      set {
          guard let newValue = newValue else {
              attributedPlaceholder = nil
              return
          }

          let attributes: [NSAttributedString.Key: Any] = [.foregroundColor: Color.textFieldPlaceholder.color]

          let attributedText = NSAttributedString(string: newValue, attributes: attributes)

          attributedPlaceholder = attributedText
      }
  }
}

huynguyen
  • 7,616
  • 5
  • 35
  • 48
0

open your identity inspector by selecting text field and then put " placeholderLabel.textColor " in key path by pressing + button . Give the type " Color " and in value select desired RGB color.

shivi
  • 1
0

Swift 5

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