Skip to content

Center Alignment of Text in FloatLabelTextField misplaces title #21

@kishykumar

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)
    }

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