2

I am trying to change the height of a NSTextField for a macOS application.

How can I change the height of my NSTextField and have the text centered vertically like a normal UITextField on iOS. I added a height constraint to try and make it bigger and nothing will change.

class LoginVC: NSViewController {

@IBOutlet weak var emailField: TF!
@IBOutlet weak var passwordField: STF!

override func viewDidLoad() {
    super.viewDidLoad()

    emailField.usesSingleLineMode = true
    emailField.placeholderAttributedString = NSAttributedString(string: "Email", attributes: [NSForegroundColorAttributeName: NSColor.lightGray, NSFontAttributeName: NSFont(name: "Menlo", size: 20)!])
    emailField.alignment = .center

    passwordField.usesSingleLineMode = true
    passwordField.placeholderAttributedString = NSAttributedString(string: "Password", attributes: [NSForegroundColorAttributeName: NSColor.lightGray, NSFontAttributeName: NSFont(name: "Menlo", size: 20)!])
    passwordField.alignment = .center
}

override func viewWillAppear() {
    super.viewWillAppear()

    view.layer?.backgroundColor = EYE_GREY.cgColor
}

}

enter image description here

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
samuscarella
  • 89
  • 2
  • 9
  • Why would you want set an attributed string with a font size of 20.0 to a text field? I find your work very underdeveloped. – El Tomato Sep 16 '17 at 04:39
  • Why don't you set the font in the storyboard? This will automatically resize the height of the text field. – Willeke Sep 16 '17 at 10:38
  • Not a direct answer to your question but the reason why your layout is broken is because you use `usesSingleLineMode` with a font other than the system font. Either don't set this mode (even if you just want a single line of text, you can enforce that by other means if you have to) or use the system font. See here: https://stackoverflow.com/a/49784243/15809 – Mecki Apr 11 '18 at 20:52

0 Answers0