简体   繁体   中英

placeholder text and typed in text not aligning in UITextField

I have a UITextfield with some placeholder text.

I have used both alignment properties. However, the palceholder text is still a little on the upper side. Can anyone help me out ?

Thanks.

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

Here's my solution in MonoTouch for having to fix the alignment of placeholder vs text. As I noted in the comments -- there are three distinct pieces: the editing, the static text, and the static placeholder. The equivalent in Objective-C is drawTextInRect: and drawPlaceholderInRect: .

My solution was to subclass and offset the mis-aligned items. Unfortunately the issue seems to be related to the font-file itself and how editing mode affects it, vs. how iOS renders static text.

Hope this helps someone.

Allison

public class MyTextField : UITextField
{
    public MyTextField(RectangleF rect) : base(rect)
    {
    }

    public override void DrawPlaceholder(RectangleF rect)
    {
        base.DrawPlaceholder(new RectangleF(rect.X, rect.Y + 2, rect.Width, rect.Height));
    }

    public override void DrawText(RectangleF rect)
    {
        base.DrawText(new RectangleF(rect.X, rect.Y + 2, rect.Width, rect.Height));
    }
}

According to UITextField documentation you should be using the textAlignment property. As the docs state "This property applies to the both the main text string and the placeholder string. The default value of this property is UITextAlignmentLeft."

So your code would look something like:

textfield.textAlignment = UITextAlignmentLeft

or depending on how you want to align the text you could use UITextAlignmentCenter or UITextAlignmentRight after the = sign.

I'd say play around with the Y Size of you used CGRectMake to create your frame. Maybe make it smaller?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM