Skip to content

Center Alignment of Text in FloatLabelTextField misplaces title #21

Description

@kishykumar

The bug is that center aligning in the textfield misplaces the title/placeholder.
But, it worked for the FloatLabelTextView.

As I didn't want to spend much time reading the logic for title placement, I just compared the setTitlePositionForTextAlignment functions in FloatLabelTextField.swift and FloatLabelTextView.swift.

Here is the fix in case anyone is looking for it:

private func setTitlePositionForTextAlignment() {
        let r = textRectForBounds(bounds)
        var x = r.origin.x
        if textAlignment == NSTextAlignment.Center {
//          x = r.origin.x + (r.size.width * 0.5) - title.frame.size.width <-- **comment out this line and add the next line.**
            x = (frame.size.width - title.frame.size.width) * 0.5 // <-- add this
        } else if textAlignment == NSTextAlignment.Right {
            x = r.origin.x + r.size.width - title.frame.size.width
        }
        title.frame = CGRect(x:x, y:title.frame.origin.y, width:title.frame.size.width, height:title.frame.size.height)
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions