//
初始化並定義大小
uitextview *textview = [[uitextview
alloc] initwithframe:cgrectmake(20, 10, 280, 30)];
textview.backgroundcolor=[uicolor whitecolor
];//
背景色
textview.
scrollenabled=no
; //當文字超過檢視的邊框時是否允許滑動,預設為「yes」
textview.editable = yes;
//「yes」
textview.delegate = self;
//設定**方法的實現類
textview.font=[uifont
fontwithname:@"arial"
size:18.0]; //
設定字型名字和字型大小;
textview.
returnkeytype
= uireturnkeydefault;
//return
鍵的型別
textview.
keyboardtype
= uikeyboardtypedefault;
//鍵盤型別
textview.
textalignment
= nstextalignmentleft;
//文字顯示的位置預設為居左
textview.
datadetectortypes
=uidatadetectortypeall
; //顯示資料型別的連線模式(如**號碼、**、位址等)
textview.textcolor = [uicolor
blackcolor];
textview.text = @"uitextview
詳解";//
設定顯示的文字內容
[self.view
addsubview:textview];
uitextview的**方法如下:
//將要開始編輯
- (bool)textviewshouldbeginediting:(uitextview *)textview;
//將要結束編輯
- (bool)textviewshouldendediting:(uitextview *)textview;
//開始編輯
- (void)textviewdidbeginediting:(uitextview *)textview;
//結束編輯
- (void)textviewdidendediting:(uitextview *)textview;
//內容將要發生改變編輯
- (bool)textview:(uitextview *)textview shouldchangetextinrange:(nsrange)range replacementtext:(nsstring*)text;
//內容發生改變編輯
- (void)textviewdidchange:(uitextview *)textview;
//焦點發生改變
- (void)textviewdidchangeselection:(uitextview *)textview;
有時候我們要控制項自適應輸入的文字的內容的高度,只要在textviewdidchange的**方法中加入調整控制項大小的**即可
- (void)textviewdidchange:(uitextview *)textview
控制輸入文字的長度和內容,可通呼叫以下**方法實現
- (bool)textview:(uitextview *)textview shouldchangetextinrange:(nsrange)range replacementtext:(nsstring*)text
if ([text isequaltostring:@"\n"])
else
}uitextview退出鍵盤的幾種方式
因為iphone的軟鍵盤沒有自帶的退鍵盤鍵,所以要實現退出鍵盤需要自己實現,有如下幾種方式:
1)如果你程式是有導航條的,可以在導航條上面加多乙個done的按鈕,用來退出鍵盤,當然要先實uitextviewdelegate。
- (void)textviewdidbeginediting:(uitextview *)textview
- (void)textviewdidendediting:(uitextview *)textview
- (void)dismisskeyboard
2)如果你的textview裡不用回車鍵,可以把回車鍵當做退出鍵盤的響應鍵。
**如下:
-(bool)textview:(uitextview *)textview shouldchangetextinrange:(nsrange)range replacementtext:(nsstring*)text
return yes;
}3)還有你也可以自定義其他載入鍵盤上面用來退出,比如在彈出的鍵盤上面加乙個view來放置退出鍵盤的done按鈕。
**如下:
ui******* * topview = [[ui*******
alloc]initwithframe:cgrectmake(0, 0, 320,30)];
[topview setbarstyle:uibarstyleblack];
uibarbuttonitem
*btnspace = [[
uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace
target:self
action:nil];
uibarbuttonitem *donebutton = [[uibarbuttonitem alloc
]initwithtitle
:@"done"
style:uibarbuttonitemstyledone
target:self
action:@selector(dismisskeyboard)];
nsarray * buttonsarray = @[btnspace, donebutton];;
[donebutton release];
[btnspace release];
[topview setitems:buttonsarray];
[textview setinputaccessoryview:topview];//當文字輸入框加上
topview
[topview release];
topview = nil;
-(ibaction)dismisskeyboard
UITextView使用技巧 一
import viewcontroller.h import implementation viewcontroller synthesize mytextview void viewdidload void didreceivememorywarning void dealloc textview...
UITextView使用筆記
uitextview初始化 uitextview inputcontexttf uitextview alloc initwithframe cgrectmake contactphoneimage.frame.origin.x,contactphoneimage.frame.origin.y co...
iOS開發 UITextView使用詳解
一 限制輸入字數 方案一 只能判斷非聯想輸入,pass bool textview uitextview textview shouldchangetextinrange nsrange range replacementtext nsstring text方案二 通用方法 void textvie...