FahimF / FloatLabelFields

Text entry controls which contain a built-in title/label so that you don't have to add a separate title for each field.
MIT License
1.19k stars 107 forks source link

Center Alignment of Text in FloatLabelTextField misplaces title #21

Open kishykumar opened 7 years ago

kishykumar commented 7 years ago

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)
    }
kmuddapu commented 7 years ago

I'm facing the same. Please help me !!

screen shot 2017-04-01 at 10 04 34 pm