Use of IOS Uitextfield

Source: Internet
Author: User

Before, for the use of Uitextfield no specific research, sometimes always feel the limitations of use, in fact, some attributes before not know, today for Uitextfield do a summary. one, the border display 1, the system default border

Uitextfield *textfieldsystem = [[Uitextfield alloc] Initwithframe:cgrectmake (Ten, self.view.frame.size.width-20, 30 )];
Textfieldsystem.placeholder = @ "Please enter text";
Textfieldsystem.borderstyle = Uitextborderstyleroundedrect; Sets the type of border
[Self.view Addsubview:textfieldsystem];

The Uitextborderstyle border type is an enumeration of 4 types, the default is Uitextborderstylenone without border type, so we need to set the type of border when we create it, otherwise we can't see anything.

typedef ns_enum (Nsinteger, Uitextborderstyle) {
    uitextborderstylenone,
    uitextborderstyleline,
    Uitextborderstylebezel,
    uitextborderstyleroundedrect
};
2, Custom border

If you think the Uitextfield of your system won't meet your needs, say your rounded corners are bigger, or you want to set the color of the border to red, you'll need to customize the Uitextfield border.

(1) Create a new class, called Yctextfield, that is integrated into Uitextfield to rewrite some of the Uitextfield methods.

#import "YCTextField.h"

@implementation Yctextfield

//Overrides to reset the placeholder area
-(CGRect) Placeholderrectforbounds: ( CGRect) bounds
{return
    cgrectinset (bounds, 8, 4);
}

Override to reset the Edge area
-(CGRect) Textrectforbounds: (cgrect) bounds
{return
    cgrectinset (bounds, 18,4);
}

Rewrite to reset the editing area
-(CGRect) Editingrectforbounds: (cgrect) bounds
{return
    cgrectinset (bounds, 4);
}

Small series, from the official document to see that there are several methods are open sen, copy over to want to use directly. The results have not been seen, and later found that Apple's official documentation specifically explains that these methods need to be rewritten to not be called directly.

(2) Custom border properties

Remember, import header file YCTextField.h

Yctextfield *textfield = [[Yctextfield alloc] Initwithframe:cgrectmake (Ten, self.view.frame.size.width-20,)];
    Textfield.placeholder = @ "Please enter text";
    TextField.layer.borderColor = [Uicolor Redcolor]. Cgcolor; Set the color of the border
    textField.layer.borderWidth = 1.0f;//Set the width of the border
    TextField.layer.cornerRadius = 12.0f;//Set the rounded corner size of the border
    [Self.view Addsubview:textfield];
second, the other attributes 1. Set the fork number in the input box
    Textfieldsystem.clearbuttonmode = uitextfieldviewmodewhileediting; 

Uitextfieldviewmode is an enumeration type

typedef ns_enum (Nsinteger, Uitextfieldviewmode) {
    uitextfieldviewmodenever,
    Uitextfieldviewmodewhileediting,
    uitextfieldviewmodeunlessediting,
    uitextfieldviewmodealways
};
2, set the type of keyboard
    Textfieldsystem.keyboardtype = Uikeyboardtypenumberpad;

Uikeyboardtype is an enumeration type

typedef enum {
    Uikeyboardtypedefault,         default keyboard, support for all character         
    uikeyboardtypeasciicapable,         default keyboard to support ASCII
    uikeyboardtypenumbersandpunctuation, standard telephone keypad, support for +*# characters
    Uikeyboardtypeurl,                    URL keyboard, support. com button only support URL character
    uikeyboardtypenumberpad,            numeric keypad
    Uikeyboardtypephonepad,          telephone keypad
    Uikeyboardtypenamephonepad,        telephone keypad, also support input names
    Uikeyboardtypeemailaddress,        keyboard uikeyboardtypedecimalpad for entering e-mail addresses     
    , numeric        keypad with numbers and decimal
    points Uikeyboardtypetwitter,        optimized keyboard for easy input @, #字符
    Uikeyboardtypealphabet = uikeyboardtypeasciicapable, 
} Uikeyboardtype;   
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.