Search code examples
swiftuitextfieldtvos

tvOS Secure Text Field , Text is not Vertically Centered


In apple TV application , I wanted to make Password text filed secured so I checked the Secure Text Entry property in the Attributes inspector , but the text vertical alignment of the text became not in the center as in this image text is not vertically centered , I tried setting the contentVerticalalignment = center but still not working , any help please


Solution

  • 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.