Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 282
Ahsan Avatar asked Jul 12 '12 22:07

Ahsan


People also ask

What is a placeholder string in uitextfielddelegate?

var delegate: UITextFieldDelegate? The string that displays when there is no other text in the text field. This value is nil by default. The placeholder string is drawn using a system-defined color. The text that the text field displays. The styled text that the text field displays.

How to align placeholder text of an input field in HTML?

How to Align the Placeholder Text of an Input Field in HTML. The ::placeholder pseudo-element allows styling the placeholder text of a form element. It can change from browser to browser. The placeholder attribute is used to describe the expected value of an input field. Before entering a value, there is a short hint displayed in the field.

What happens to the placeholder when you type in a field?

When the user start editing the text, the cursor shows up at the start of the field but the placeholder is still there until they type a character Once they type a character in the field (i.e., when it has contents) the placeholder text disappears and the text color gets darker

Can I use placeholder text in UITextView?

UITextField has pretty support for placeholder text. UITextView doesn’t. So if you need a multi-line editable text component, you don’t get a pretty placeholder. Here’s how to roll your own. This tutorial has been updated to Swift 2.0. First, requirements.


2 Answers

textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
like image 76
chandu Avatar answered Oct 05 '22 06:10

chandu


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));
    }
}
like image 39
Allison A Avatar answered Oct 05 '22 06:10

Allison A