I wanted to make a secured password entry field so I checked the Secure Text Entry
property in the Attributes Inspector, but the vertical alignment of the text became uncentered as shown in this image.
I tried setting the contentVerticalAlignment = .center
, but it's still not working.
Maybe there is a better way to fix, but I hacked with textRect(forBounds:)
in a UITextField
subclass:
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
guard isSecureTextEntry else {
return super.placeholderRect(forBounds: bounds)
}
return super.textRect(forBounds: bounds)
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
guard isSecureTextEntry else {
return super.textRect(forBounds: bounds)
}
return bounds.applying(.init(translationX: 0, y: 10))
}
You might need to tweak the translation to fit you font and sizes.
Note: This isn't necessary on iOS 14.