iOS starts--ui Uitextfield

Source: Internet
Author: User

Create a text input box

    Uitextfield * TextField = [[Uitextfield alloc]initwithframe:cgrectmake (40 )];

Set Border Style

    Textfield.borderstyle = uitextborderstyleroundedrect;     /*      typedef ns_enum (Nsinteger, Uitextborderstyle) {     uitextborderstylenone, no effect     Uitextborderstyleline, linear border has shadow     Uitextborderstylebezel,     uitextborderstyleroundedrect rounded Rectangle     };      */

Set text-related

    @" 2002 years of the first snowfall " ;     // set font-related    Textfield.font = [Uifont systemfontofsize:];     = [Uicolor greencolor];     // Set Adaptive    Textfield.adjustsfontsizetofitwidth = YES;     // Set Center //     textfield.textalignment = nstextalignmentcenter;

Set prompt

    // Set Prompt    @" Please enter " ;         // set whether to clear start editing //     textfield.clearsonbeginediting = YES;

Set/Cancel First responder

    // Click the input box to make the input box the first    responder // the so-called First responder  is the control that will  be edited to manipulate the touch     [TextField    becomefirstresponder]; // Cancel to be the first responder    [TextField Resignfirstresponder];

Whether the interaction is turned on

    // Cancel interaction is turned    on textfield.userinteractionenabled = YES;

Set the Purge button

    // Set the purge button    Textfield.clearbuttonmode = uitextfieldviewmodealways;     /*      typedef ns_enum (Nsinteger, Uitextfieldviewmode) {     uitextfieldviewmodenever,  never     Uitextfieldviewmodewhileediting,  uitextfieldviewmodeunlessediting when editing     ,  not displayed     when editing Uitextfieldviewmodealways  always show     };      */

Set both left and right view

    //********** The same view cannot be set to both left and right pictures//left ViewUIView * view = [[UIView alloc]initwithframe:cgrectmake (0,0, -, -)]; View.backgroundcolor=[Uicolor Bluecolor]; Textfield.leftview=view; Textfield.leftviewmode=uitextfieldviewmodealways;//Right ViewUIView * View1 = [[UIView alloc]initwithframe:cgrectmake (0,0, -, -)]; Textfield.rightview=View1; View1.backgroundcolor=[Uicolor Greencolor];//textfield.rightviewmode = uitextfieldviewmodealways;

Set keyboard style

//Set Keyboard styleTextfield.keyboardtype =Uikeyboardtypedefault; /*       typedef ns_enum (Nsinteger, Uikeyboardtype) {uikeyboardtypedefault,//Default type for the Curren     T input method. Uikeyboardtypeasciicapable,//displays a keyboard which can enter ASCII characters, NON-ASCII keyboards remain     Active uikeyboardtypenumbersandpunctuation,//Numbers and assorted punctuation.     Uikeyboardtypeurl,//A type optimized for URL entry (shows./. com prominently). Uikeyboardtypenumberpad,//A number pad (0-9).     Suitable for PIN entry.     Uikeyboardtypephonepad,//A phone pad (1-9, *, 0, #, with letters under the numbers).     Uikeyboardtypenamephonepad,//a type optimized for entering a person ' s name or phone number.     Uikeyboardtypeemailaddress,//A type optimized for multiple email address entry (shows space @. prominently).     Uikeyboardtypedecimalpad Ns_enum_available_ios (4_1),//a number pad with A decimal point. UikeyboardtypetWitter Ns_enum_available_ios (5_0),//A type optimized for Twitter text entry (easy access to @ #) Uikeyboardtype Websearch Ns_enum_available_ios (7_0),//A default keyboard type with url-oriented addition (shows space. prominently)          .      Uikeyboardtypealphabet = uikeyboardtypeasciicapable,//Deprecated};*/Textfield.returnkeytype=Uireturnkeydone; /*typedef ns_enum (Nsinteger, Uireturnkeytype) {uireturnkeydefault, Uireturnkeygo, Uireturnkeygoogle, Uireturnkeyjoin, Uireturnkeynext, Uireturnkeyroute, Uireturnkeysearch, Uireturnkeysend, UIReturnKey     Yahoo, Uireturnkeydone, Uireturnkeyemergencycall, Uireturnkeycontinue Ns_enum_available_ios (9_0),}; */

Touch screen Cancel First responder exit edit

// Touch the screen call this method -(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *)Event  {    * TextField = (ID) [self.view viewwithtag:1];    [TextField Resignfirstresponder]; //     [Self.view becomefirstresponder];}

Implementing the Uitextfielddelegate Proxy method

#pragmaMark-uitextfielddelegate//Whether you can start editing-(BOOL) textfieldshouldbeginediting: (Uitextfield *) textfield{returnYES;}//return NO to disallow editing.//already started editing- (void) Textfielddidbeginediting: (Uitextfield *) textfield{NSLog (@"already started editing");}//became first responder//Whether you can end the edit yes can no-(BOOL) textfieldshouldendediting: (Uitextfield *) textfield{returnYES;}//return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end//has finished editing- (void) Textfielddidendediting: (Uitextfield *) textfield{//You can save a draft hereNSLog (@"has finished editing");}//May is called if forced even if shouldendediting returns NO (e.g. view removed from window) or Endediting:yes called< /c5>-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *)string{    returnYES;}//return NO to the change text//triggers this method to return the possibility of clearing-(BOOL) Textfieldshouldclear: (Uitextfield *) textfield{returnNO;}//called when Clear button pressed. Return No. to ignore (no notifications)- (void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{    }//Click Return on the keyboard to perform this method-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{returnYES;}//called when ' return ' key pressed. Return No. to ignore.

iOS starts--ui Uitextfield

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.