1

I am using attributed text property to set custom placeholder font in UITExtfield. But my placeholder text is not vertically centered.Here is preview

        let font = UIFont(name: Font.myFont.name, size: 15)!
        let attributes = [
            NSForegroundColorAttributeName: UIColor.cadetGrey,
            NSFontAttributeName: font
        ]

        exampleTextField.attributedPlaceholder = NSAttributedString(string: "Example Placeholder",
                                                                  attributes: attributes)

I tried to add

let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center

to attribute still no luck. I also tried to use SizeToFit() on my UITextBox but no luck at all

rmaddy
  • 314,917
  • 42
  • 532
  • 579
StaticV0id
  • 411
  • 4
  • 13
  • `.alignment = .center`, that's for left/right/center, horizontal alignment, not vertical one. Check that: https://stackoverflow.com/questions/3665385/placeholder-text-not-centered-for-uitextfield-created-programmatically `contentVerticalAlignment` – Larme May 30 '17 at 15:42
  • Did you figure this out? If yes, please post the answer. – Tejas K Dec 27 '18 at 20:22

3 Answers3

1

This worked for me in Swift 5:

let unitText = NSAttributedString(string: "\(unit)", attributes: [NSAttributedString.Key.foregroundColor: MyColors.doveGray, NSAttributedString.Key.baselineOffset:2])
Malik Kulsoom
  • 136
  • 2
  • 6
0

Try like this :

let centeredParagraphStyle = NSMutableParagraphStyle()
centeredParagraphStyle.alignment = .center

let attributes: [String : Any] = [NSParagraphStyleAttributeName: centeredParagraphStyle]
iOS.Lover
  • 5,923
  • 21
  • 90
  • 162
  • Thank you, I tried that already, forgot in post to put line where i put that style in attributes together with rest of attributes. Still no luck :( – StaticV0id May 30 '17 at 15:31
0

This is will set both placeholder and text input by user in center.

 exampleTextField.textAlignment = .center

To center both vertically and horizontally.

exampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center

exampleTextField.textAlignment = .center

OR

This is will only set placeholder in center whereas the text input by user will remain at it's default position i.e left align

let font = UIFont(name: Font.myFont.name, size: 15)!

 let centeredParagraphStyle = NSMutableParagraphStyle()
 centeredParagraphStyle.alignment = .center

 // add attribute of NSMutableParagraphStyle
 let attributes = [
            NSForegroundColorAttributeName: UIColor.gray,
            NSFontAttributeName: font,
            NSParagraphStyleAttributeName: centeredParagraphStyle
        ]

 exampleTextField.attributedPlaceholder = NSAttributedString(string: "Example Placeholder",
                                                                    attributes: attributes)
Maddy
  • 1,660
  • 11
  • 24
  • My placeholder needs to be centered verticaly. You can see on picture that placeholder is near top, not in the middle – StaticV0id May 30 '17 at 15:58