简体   繁体   中英

Xamarin iOS UITextField padding with Border Style None

I am trying to add padding to UITextField. I did some research and found the following code.

UIView paddingView = new UIView(new RectangleF(0, 0, 5, 20));
txtBoxUsername.LeftView = paddingView;
txtBoxUsername.LeftViewMode = UITextFieldViewMode.Always;

This code works fine till there is a border for UITextField. My UiTextField does not have border. As soon as I set Border None this padding effect is also lost. I am setting border none with following code.

txtBoxUsername.BorderStyle = UITextBorderStyle.None;

Can anybody please tell me how to add padding to UITextField with no borders.

Following code worked for me

public class TextBox : UITextField
{
    public TextBox(string placeholder, UIFont font = null)
    {
        Layer.BorderWidth = 0f;
        AttributedPlaceholder = new NSAttributedString(
                            placeholder,
                            font: UIFont.ItalicSystemFontOfSize(15.0f)
                            );
        if (font != null)
        {
            Font = font;
        }
        UIView paddingView = new UIView(new CGRect(0, 0, 10.0f, 20.0f));
        LeftView = paddingView;
        LeftViewMode = UITextFieldViewMode.Always;
    }


}

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