ios中uitextfield 使用全面解析

字號(hào):


    //初始化textfield并設(shè)置位置及大小
    uitextfield *text = [[uitextfield alloc]initwithframe:cgrectmake(20, 20, 130, 30)];
    //設(shè)置邊框樣式,只有設(shè)置了才會(huì)顯示邊框樣式 
    text.borderstyle = uitextborderstyleroundedrect;
    typedef enum {
    uitextborderstylenone,
    uitextborderstyleline,
    uitextborderstylebezel,
    uitextborderstyleroundedrect
    } uitextborderstyle;
    //設(shè)置輸入框的背景顏色,此時(shí)設(shè)置為白色 如果使用了自定義的背景圖片邊框會(huì)被忽略掉 
    text.backgroundcolor = [uicolor whitecolor];
    //設(shè)置背景
    text.background = [uiimage imagenamed:@dd.png];
    //設(shè)置背景
    text.disabledbackground = [uiimage imagenamed:@cc.png];
    //當(dāng)輸入框沒(méi)有內(nèi)容時(shí),水印提示 提示內(nèi)容為password
    text.placeholder = @password;
    //設(shè)置輸入框內(nèi)容的字體樣式和大小
    text.font = [uifont fontwithname:@arial size:20.0f];
    //設(shè)置字體顏色
    text.textcolor = [uicolor redcolor];
    //輸入框中是否有個(gè)叉號(hào),在什么時(shí)候顯示,用于一次性刪除輸入框中的內(nèi)容
    text.clearbuttonmode = uitextfieldviewmodealways;
    typedef enum {
    uitextfieldviewmodenever, 重不出現(xiàn)
    uitextfieldviewmodewhileediting, 編輯時(shí)出現(xiàn)
    uitextfieldviewmodeunlessediting, 除了編輯外都出現(xiàn)
    uitextfieldviewmodealways  一直出現(xiàn)
    } uitextfieldviewmode;
    //輸入框中一開(kāi)始就有的文字
    text.text = @一開(kāi)始就在輸入框的文字;
    //每輸入一個(gè)字符就變成點(diǎn) 用語(yǔ)密碼輸入
    text.securetextentry = yes;
    //是否糾錯(cuò)
    text.autocorrectiontype = uitextautocorrectiontypeno;
    typedef enum {
    uitextautocorrectiontypedefault, 默認(rèn)
    uitextautocorrectiontypeno,  不自動(dòng)糾錯(cuò)
    uitextautocorrectiontypeyes, 自動(dòng)糾錯(cuò)
    } uitextautocorrectiontype;
    //再次編輯就清空
    text.clearsonbeginediting = yes;
    //內(nèi)容對(duì)齊方式
    text.textalignment = uitextalignmentleft;
    //內(nèi)容的垂直對(duì)齊方式 uitextfield繼承自u(píng)icontrol,此類中有一個(gè)屬性contentverticalalignment
    text.contentverticalalignment = uicontrolcontentverticalalignmentcenter;
    //設(shè)置為yes時(shí)文本會(huì)自動(dòng)縮小以適應(yīng)文本窗口大小.默認(rèn)是保持原來(lái)大小,而讓長(zhǎng)文本滾動(dòng) 
    textfied.adjustsfontsizetofitwidth = yes;
    //設(shè)置自動(dòng)縮小顯示的最小字體大小
    text.minimumfontsize = 20;
    //設(shè)置鍵盤的樣式
    text.keyboardtype = uikeyboardtypenumberpad;
    typedef enum {
    uikeyboardtypedefault,  默認(rèn)鍵盤,支持所有字符
    uikeyboardtypeasciicapable, 支持ascii的默認(rèn)鍵盤
    uikeyboardtypenumbersandpunctuation, 標(biāo)準(zhǔn)電話鍵盤,支持+*#字符
    uikeyboardtypeurl, url鍵盤,支持.com按鈕 只支持url字符
    uikeyboardtypenumberpad,  數(shù)字鍵盤
    uikeyboardtypephonepad,   電話鍵盤
    uikeyboardtypenamephonepad,  電話鍵盤,也支持輸入人名
    uikeyboardtypeemailaddress,  用于輸入電子 郵件地址的鍵盤
    uikeyboardtypedecimalpad,  數(shù)字鍵盤 有數(shù)字和小數(shù)點(diǎn)
    uikeyboardtypetwitter,  優(yōu)化的鍵盤,方便輸入@、#字符
    uikeyboardtypealphabet = uikeyboardtypeasciicapable,
    } uikeyboardtype;
    //首字母是否大寫
    text.autocapitalizationtype = uitextautocapitalizationtypenone;
    typedef enum {
    uitextautocapitalizationtypenone, 不自動(dòng)大寫
    uitextautocapitalizationtypewords, 單詞首字母大寫
    uitextautocapitalizationtypesentences, 句子的首字母大寫
    uitextautocapitalizationtypeallcharacters, 所有字母都大寫
    } uitextautocapitalizationtype;
    //return鍵變成什么鍵
    text.returnkeytype =uireturnkeydone;
    typedef enum {
    uireturnkeydefault, 默認(rèn) 灰色按鈕,標(biāo)有return
    uireturnkeygo,  標(biāo)有g(shù)o的藍(lán)色按鈕
    uireturnkeygoogle,標(biāo)有g(shù)oogle的藍(lán)色按鈕,用語(yǔ)搜索
    uireturnkeyjoin,標(biāo)有join的藍(lán)色按鈕
    uireturnkeynext,標(biāo)有next的藍(lán)色按鈕
    uireturnkeyroute,標(biāo)有route的藍(lán)色按鈕
    uireturnkeysearch,標(biāo)有search的藍(lán)色按鈕
    uireturnkeysend,標(biāo)有send的藍(lán)色按鈕
    uireturnkeyyahoo,標(biāo)有yahoo的藍(lán)色按鈕
    uireturnkeyyahoo,標(biāo)有yahoo的藍(lán)色按鈕
    uireturnkeyemergencycall, 緊急呼叫按鈕
    } uireturnkeytype;
    //鍵盤外觀
    textview.keyboardappearance=uikeyboardappearancedefault;
    typedef enum {
    uikeyboardappearancedefault, 默認(rèn)外觀,淺灰色
    uikeyboardappearancealert,   深灰 石墨色
    } uireturnkeytype;
    //設(shè)置代理 用于實(shí)現(xiàn)協(xié)議
    text.delegate = self;
    //把textfield加到視圖中
    [self.window addsubview:text];
    //最右側(cè)加圖片是以下代碼  左側(cè)類似
    uiimageview *image=[[uiimageview alloc] initwithimage:[uiimage imagenamed:@right.png]];
    text.rightview=image;
    text.rightviewmode = uitextfieldviewmodealways;
    typedef enum {
    uitextfieldviewmodenever,
    uitextfieldviewmodewhileediting,
    uitextfieldviewmodeunlessediting,
    uitextfieldviewmodealways
    } uitextfieldviewmode;
    //按return鍵鍵盤往下收 becomefirstresponder
    類要采用uitextfielddelegate協(xié)議
    text.delegate = self; 聲明text的代理是我,我會(huì)去實(shí)現(xiàn)把鍵盤往下收的方法 這個(gè)方法在uitextfielddelegate里所以我們要采用uitextfielddelegate這個(gè)協(xié)議
    - (bool)textfieldshouldreturn:(uitextfield *)textfield
    {
    [text resignfirstresponder]; //主要是[receiver resignfirstresponder]在哪調(diào)用就能把receiver對(duì)應(yīng)的鍵盤往下收
    return yes;
    }
    重寫繪制行為
    除了uitextfield對(duì)象的風(fēng)格選項(xiàng),你還可以定制化uitextfield對(duì)象,為他添加許多不同的重寫方法,來(lái)改變文本字段的顯示行為。這些方法都會(huì)返回一個(gè)cgrect結(jié)構(gòu),制定了文本字段每個(gè)部件的邊界范圍。以下方法都可以重寫。
    – textrectforbounds:  //重寫來(lái)重置文字區(qū)域
    – drawtextinrect:  //改變繪文字屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù),就不用調(diào)用super了.
    – placeholderrectforbounds://重寫來(lái)重置占位符區(qū)域
    – drawplaceholderinrect://重寫改變繪制占位符屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù),就不用調(diào)用super了.
    – borderrectforbounds://重寫來(lái)重置邊緣區(qū)域
    – editingrectforbounds://重寫來(lái)重置編輯區(qū)域
    – clearbuttonrectforbounds://重寫來(lái)重置clearbutton位置,改變size可能導(dǎo)致button的圖片失真
    – leftviewrectforbounds:
    – rightviewrectforbounds:
    委托方法
    - (bool)textfieldshouldbeginediting:(uitextfield *)textfield{ 
    //返回一個(gè)bool值,指定是否循序文本字段開(kāi)始編輯 
    return yes; 
    } 
    - (void)textfielddidbeginediting:(uitextfield *)textfield{ 
    //開(kāi)始編輯時(shí)觸發(fā),文本字段將成為first responder 
    } 
    - (bool)textfieldshouldendediting:(uitextfield *)textfield{ 
    //返回bool值,指定是否允許文本字段結(jié)束編輯,當(dāng)編輯結(jié)束,文本字段會(huì)讓出first responder 
    //要想在用戶結(jié)束編輯時(shí)阻止文本字段消失,可以返回no 
    //這對(duì)一些文本字段必須始終保持活躍狀態(tài)的程序很有用,比如即時(shí)消息 
    return no; 
    } 
    - (bool)textfield:(uitextfield*)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string{ 
    //當(dāng)用戶使用自動(dòng)更正功能,把輸入的文字修改為推薦的文字時(shí),就會(huì)調(diào)用這個(gè)方法?!?BR>    //這對(duì)于想要加入撤銷選項(xiàng)的應(yīng)用程序特別有用 
    //可以跟蹤字段內(nèi)所做的最后一次修改,也可以對(duì)所有編輯做日志記錄,用作審計(jì)用途。
    //要防止文字被改變可以返回no 
    //這個(gè)方法的參數(shù)中有一個(gè)nsrange對(duì)象,指明了被改變文字的位置,建議修改的文本也在其中 
    return yes; 
    } 
    - (bool)textfieldshouldclear:(uitextfield *)textfield{ 
    //返回一個(gè)bool值指明是否允許根據(jù)用戶請(qǐng)求清除內(nèi)容 
    //可以設(shè)置在特定條件下才允許清除內(nèi)容 
    return yes; 
    } 
    -(bool)textfieldshouldreturn:(uitextfield *)textfield{ 
    //返回一個(gè)bool值,指明是否允許在按下回車鍵時(shí)結(jié)束編輯 
    //如果允許要調(diào)用resignfirstresponder 方法,這回導(dǎo)致結(jié)束編輯,而鍵盤會(huì)被收起[textfield resignfirstresponder];
    //查一下resign這個(gè)單詞的意思就明白這個(gè)方法了 
    return yes; 
    } 
    通知
    uitextfield派生自u(píng)icontrol,所以u(píng)icontrol類中的通知系統(tǒng)在文本字段中也可以使用。除了uicontrol類的標(biāo)準(zhǔn)事件,你還可以使用下列uitextfield類特有的事件
    uitextfieldtextdidbegineditingnotification
    uitextfieldtextdidchangenotification
    uitextfieldtextdidendeditingnotification
    當(dāng)文本字段退出編輯模式時(shí)觸發(fā)。通知的object屬性存儲(chǔ)了最終文本。
    因?yàn)槲谋咀侄我褂面I盤輸入文字,所以下面這些事件發(fā)生時(shí),也會(huì)發(fā)送動(dòng)作通知
    uikeyboardwillshownotification  //鍵盤顯示之前發(fā)送
    uikeyboarddidshownotification  //鍵盤顯示之后發(fā)送
    uikeyboardwillhidenotification  //鍵盤隱藏之前發(fā)送
    uikeyboarddidhidenotification  //鍵盤隱藏之后發(fā)送
    名單
    1、text :設(shè)置文本框的默認(rèn)文本。
    2、placeholder : 可以在文本框中顯示灰色的字,用于提示用戶應(yīng)該在這個(gè)文本框輸入什么內(nèi)容。當(dāng)這個(gè)文本框中輸入了數(shù)據(jù)時(shí),用于提示的灰色的字將會(huì)自動(dòng)消失。
    3、background :
    4、disabled : 若選中此項(xiàng),用戶將不能更改文本框內(nèi)容。
    5、接下來(lái)是三個(gè)按鈕,用來(lái)設(shè)置對(duì)齊方式。
    6、border style : 選擇邊界風(fēng)格。
    7、clear button : 這是一個(gè)下拉菜單,你可以選擇清除按鈕什么時(shí)候出現(xiàn),所謂清除按鈕就是出一個(gè)現(xiàn)在文本框右邊的小 x ,你可以有以下選擇:
    7.1 never appears : 從不出現(xiàn)
    7.2 appears while editing : 編輯時(shí)出現(xiàn)
    7.3 appears unless editing :
    7.4 is always visible : 總是可見(jiàn)
    8、clear when editing begins : 若選中此項(xiàng),則當(dāng)開(kāi)始編輯這個(gè)文本框時(shí),文本框中之前的內(nèi)容會(huì)被清除掉。比如,你現(xiàn)在這個(gè)文本框 a 中輸入了 what ,之后去編輯文本框 b,若再回來(lái)編輯文本框 a ,則其中的 what 會(huì)被立即清除。
    9、text color : 設(shè)置文本框中文本的顏色。
    10、font : 設(shè)置文本的字體與字號(hào)。
    11、min font size : 設(shè)置文本框可以顯示的最小字體(不過(guò)我感覺(jué)沒(méi)什么用)
    12、adjust to fit : 指定當(dāng)文本框尺寸減小時(shí),文本框中的文本是否也要縮小。選擇它,可以使得全部文本都可見(jiàn),即使文本很長(zhǎng)。但是這個(gè)選項(xiàng)要跟 min font size 配合使用,文本再縮小,也不會(huì)小于設(shè)定的 min font size 。
    接下來(lái)的部分用于設(shè)置鍵盤如何顯示。
    13、captitalization : 設(shè)置大寫。下拉菜單中有四個(gè)選項(xiàng):
    13.1 none : 不設(shè)置大寫
    13.2 words : 每個(gè)單詞首字母大寫,這里的單詞指的是以空格分開(kāi)的字符串
    13.3 sentances : 每個(gè)句子的第一個(gè)字母大寫,這里的句子是以句號(hào)加空格分開(kāi)的字符串
    13.4 all characters : 所以字母大寫
    14、correction : 檢查拼寫,默認(rèn)是 yes 。
    15、keyboard : 選擇鍵盤類型,比如全數(shù)字、字母和數(shù)字等。
    16、appearance:
    17、return key : 選擇返回鍵,可以選擇 search 、 return 、 done 等。
    18、auto-enable return key : 如選擇此項(xiàng),則只有至少在文本框輸入一個(gè)字符后鍵盤的返回鍵才有效。
    19、secure : 當(dāng)你的文本框用作密碼輸入框時(shí),可以選擇這個(gè)選項(xiàng),此時(shí),字符顯示為星號(hào)。
    名單
    1.alignment horizontal 水平對(duì)齊方式
    2.alignment vertical 垂直對(duì)齊方式
    3.用于返回一個(gè)bool值 輸入框是否 selected(選中) enabled(可用) highlighted(高亮)
    限制只能輸入特定的字符
    (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string{
    nscharacterset *cs;
    cs = [[nscharacterset charactersetwithcharactersinstring:numbers]invertedset];
    nsstring *filtered = [[string componentsseparatedbycharactersinset:cs]componentsjoinedbystring:@]; //按cs分離出數(shù)組,數(shù)組按@分離出字符串
    bool canchange = [string isequaltostring:filtered];
    return canchange;
    }
    上面那個(gè)numbers是一個(gè)宏,可以在文件頂部定義:
    #define numbers @”0123456789n” (這個(gè)代表可以輸入數(shù)字和換行,請(qǐng)注意這個(gè)n,如果不寫這個(gè),done按鍵將不會(huì)觸發(fā),如果用在searchbar中,將會(huì)不觸發(fā)search事件,因?yàn)槟阕约合拗撇蛔屳斎雗,好慘,我在項(xiàng)目中才發(fā)現(xiàn)的。)
    所以,如果你要限制輸入英文和數(shù)字的話,就可以把這個(gè)定義為:
    #define kalphanum @”abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789″。
    當(dāng)然,你還可以在以上方法return之前,做一提示的,比如提示用戶只能輸入數(shù)字之類的。如果你覺(jué)得有需要的話。
    限制只能輸入一定長(zhǎng)度的字符
    - (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string;
    { //string就是此時(shí)輸入的那個(gè)字符 textfield就是此時(shí)正在輸入的那個(gè)輸入框 返回yes就是可以改變輸入框的值 no相反
    if ([string isequaltostring:@n]) //按會(huì)車可以改變
    {
    return yes;
    }
    nsstring * tobestring = [textfield.text stringbyreplacingcharactersinrange:range withstring:string]; //得到輸入框的內(nèi)容
    if (self.mytextfield == textfield) //判斷是否時(shí)我們想要限定的那個(gè)輸入框
    {
    if ([tobestring length] > 20) { //如果輸入框內(nèi)容大于20則彈出警告
    textfield.text = [tobestring substringtoindex:20];
    uialertview *alert = [[[uialertview alloc] initwithtitle:nil message:@超過(guò)最大字?jǐn)?shù)不能輸入了 delegate:nil cancelbuttontitle:@ok otherbuttontitles:nil, nil] autorelease];
    [alert show];
    return no;
    }
    }
    return yes;
    }