iphone常用控件之UITextField

文本字段属性设置:

Placeholer:以灰色显示在文本框内,用来提示输入信息

Clean When Editing Begins:  输入时清空文本框

Font : 字体及大小

Adjust to Fit : 可确保真个文本可见, Min Size: 最小文本大小

KeyBoard Type: 可切换键盘显示类型

Enabled: Yes 为可编辑,No则相反

 

完成后关闭键盘的操作:

#单击Enter 键关闭键盘:  要连接 Did End On Exit事件连接到textFieldDoneEditing:操作

-(IBAction)textFieldDoneEditing:(id)sender{
    [sender resignFirstResponder];  //取消第一响应者状态
}

 

#触摸背景关闭键盘      Class字段内UIView改为UIControl, Touch Done 连接到backgroundTap:事件
-(IBAction)backgroundTap:(id)sender{
    [nameField resignFirstResponder];
    [passwordField resignFirstResponder];
}

 

 

 

无xib情况,解决弹出键盘遮蔽输入框问题,单击确定隐藏键盘:

- (void)keyboardWillShow:(NSNotification *)noti  
{          
    //键盘输入的界面调整          
    //键盘的高度  
    float height = 216.0;                  
    CGRect frame = self.view.frame;          
    frame.size = CGSizeMake(frame.size.width, frame.size.height - height);          
    [UIView beginAnimations:@"Curl"context:nil];//动画开始            
    [UIView setAnimationDuration:0.30];             
    [UIView setAnimationDelegate:self];            
    [self.view setFrame:frame];           
    [UIView commitAnimations];           
}  


- (BOOL)textFieldShouldReturn:(UITextField *)textField   
{          
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.          
    NSTimeInterval animationDuration = 0.30f;          
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];          
    [UIView setAnimationDuration:animationDuration];          
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);          
    self.view.frame = rect;          
    [UIView commitAnimations];          
    [textField resignFirstResponder];  
    return YES;          
}  

- (void)textFieldDidBeginEditing:(UITextField *)textField  
{          
    CGRect frame = textField.frame;  
    int offset = frame.origin.y + 70 - (self.view.frame.size.height - 216.0);       //键盘高度216 ,向上提多少在这里改
    NSTimeInterval animationDuration = 0.30f;                  
    [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];                  
    [UIView setAnimationDuration:animationDuration];  
    float width = self.view.frame.size.width;                  
    float height = self.view.frame.size.height;          
    if(offset > 0)  
    {  
        CGRect rect = CGRectMake(0.0f, -offset,width,height);                  
        self.view.frame = rect;          
    }          
    [UIView commitAnimations];                  
}  

 

posted @ 2012-07-06 20:15  小、  阅读(619)  评论(1)    收藏  举报