iOS開發textfield的一些方法彙總

2022-08-12 14:12:11 字數 1324 閱讀 8502

1,動態獲得textfield 的輸入內容

在- (bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string新增如下**就可以獲得全部的**。

nsstring *text = [textfield.text stringbyreplacingcharactersinrange:range withstring:string];

慢慢新增吧 

2,//只允許數字和小數點輸入

-(bool)textfield:(uitextfield *)textfield shouldchangecharactersinrange:(nsrange)range replacementstring:(nsstring *)string

nsstring *alltext = [textfield.text stringbyreplacingcharactersinrange:range withstring:string];

nsuinteger lengthofstring = string.length;

if ([string isequaltostring:@"."]) {

return yes;

for (nsinteger loopindex = 0; loopindex < lengthofstring; loopindex++) {//只允許數字輸入

unichar character = [string characteratindex:loopindex];

if (character < 48){

uialertview *alert = [[uialertview alloc] initwithtitle:@"提示" message:@"只允許輸入數字位數" delegate:self cancelbuttontitle:@"確定" otherbuttontitles:nil, nil];

[alert show];

return no; // 48 unichar for 0

if (character > 57){

uialertview *alert = [[uialertview alloc] initwithtitle:@"提示" message:@"只允許輸入數字位數" delegate:self cancelbuttontitle:@"確定" otherbuttontitles:nil, nil];

[alert show];

return no; // 57 unichar for 9

return yes;

iOS開發textfield的一些方法彙總

1,動態獲得textfield 的輸入內容 在 bool textfield uitextfield textfield shouldchangecharactersinrange nsrange range replacementstring nsstring string新增如下 就可以獲得全部...

ios修改textField的小技巧

textfield setvalue uicolor redcolor forkeypath placeholderlabel.textcolor textfield setvalue uifont boldsystemfontofsize 16 forkeypath placeholderlabe...

ios設定TextField游標顏色

今天遇到個奇怪的問題,textfield不知何時消失了?其實是變成白色了,設定textfield的背景色就可以看出來,至於為什麼會這樣還沒找到原因 只能設定游標顏色 1 textfield setvalue uicolor redcolor forkeypath placeholderlabel.t...