UITextField的一些用法

开始先说说导航NavigationController 上面添加按钮两种方法

1:

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]

  initWithBarButtonSystemItem:UIBarButtonSystemItemAction

  target:self action:@selector(ids:)] autorelease];

2:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"反馈" style:UIBarButtonItemStylePlain target:nil action:nil];

self.navigationItem.leftBarButtonItem = backButton;

 


2

[backButton release];
textField = [[UITextField alloc] initWithFrame:CGRectMake(x,y,w,h)];初始化UITextField
[textField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型按住苹果键可以进根类看枚举出的各个样式
textField.placeholder = @"hello"; //默认显示的字
textField.secureTextEntry = YES; //密码形式
textField.autocorrectionType = UITextAutocorrectionTypeNo;
是告诉Settings不需要自动更正输入到该文本中的值。
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
该文本字段不要尝试自动大写用户输入的内容
textField.returnKeyType = UIReturnKeyDone;更改响应return的按钮
textField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
textField.delegate = self;自定义return模拟键盘
要实现的Delegate方法,关闭键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
     {        
            [self.textField resignFirstResponder];
        return YES;
     }
最右侧加图片是以下代码,
    UIImageView *img=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
    text.rightView=img;
    text.rightViewMode = UITextFieldViewModeAlways;    
如果是在最左侧加图片就换成:
text.leftView=imgv;
text.leftViewMode = UITextFieldViewModeAlways;    
UITextField 继承自 UIControl,此类中有一个属性contentVerticalAlignment
所以想让UITextField里面的text垂直居中可以这样写:
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

如何用程序删除文本框中选中的文本
[textView delete: nil];
如何限制文本框只能输入数字:
建立NSNumberFormatter的子类,增加这个方法,将formatter链接至文本框。
 
- (BOOL) isPartialStringValid: (NSString **) partialStringPtr
        proposedSelectedRange: (NSRangePointer) proposedSelRangePtr
               originalString: (NSString *) origString
        originalSelectedRange: (NSRange) origSelRange
             errorDescription: (NSString **) error
{
    NSCharacterSet *nonDigits;
    NSRange newStuff;
    NSString *newStuffString;
            
    nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
    newStuff = NSMakeRange(origSelRange.location,
                           proposedSelRangePtr->location
                           - origSelRange.location);
    newStuffString = [*partialStringPtr substringWithRange: newStuff];
            
    if ([newStuffString rangeOfCharacterFromSet: nonDigits
                                                                                             options: NSLiteralSearch].location != NSNotFound) {
        *error = @"不是数字";
        return (NO);
    } else {
        *error = nil;
        return (YES);
    }
            
}
从文本框获取十六进制数据的代码
char singleNumberString[3] = {'\0','\0','\0'};
uint32_t singleNumber = 0;
uint32_t i = 0;
 NSMutableData *data = [NSMutableData data];
 //从文本框获取到得数据

 const char *buf = [[_hexToSendTextField text] UTF8String];
 //转换为十六进制

 for(i = 0; i < strlen(buf); i+=2)
 {
 if(((i+1) < len && isxdigit(buf) && (isxdigit(buf[i+1])))
 {
 singleNumberString[0] = buf;
 singleNumberString[1] = buf[i+1];
 sscanf(singleNumberString, "%x", &singleNumber);
 [data appendBytes:(void*)(&tmp) length:1];
 }
 else
 {
 break;
 }
 }
 //输出

 NSLog(@"%@", data);

点击 UITextView 输入文字,光标都从最初点开始

- (void)textViewDidChangeSelection:(UITextView *)textView
{
    NSRange range;
    range.location = 0;
    range.length = 0;
    textView.selectedRange = range;
}

软键盘
在登录页面要实现用户名和密码,密码要是点点格式,引入当前页面光标要停留在用户名选项,软键盘要弹出界面。
弹出键盘:
[username becomeFirstResponder];
取消键盘:
[username resignFirstResponder];
密码保护:
password.secureTextEntry=YES;

使用UITextFieldDelegate来隐藏键盘
在iPhone界面上,时常会需要当用户输入完内容后,隐藏键盘。 当然有很多方法,今天只介绍使用UITextFieldDelegate这个协议实现隐藏键盘。
其实很简单, 需要三步:
1. 在你的控制器类中,加入UITextFieldDelegate这个协议
如:@interface AddItemViewController : UIViewController <UITextFieldDelegate>
2. 在使用了UITextFieldDelegate协议的控制器类的实现中,加入- (BOOL)textFieldShouldReturn:方法。
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
 
        [textField resignFirstResponder];
        return YES;
}
 //设置焦点:

[UITextField becomeFirstResponder];
3. 将xib文件中的TextField控件的delegate变量指向到之前使用UITextFieldDelegate协议的那个控制器类,将 TextField的delegate IBOutlet变量右键链接到前面的控制器类的实例上。或者使用代码方式,指定相关TextField的delegate变量。
- (void)viewDidLoad
{
    [super viewDidLoad];
        itemNameField.delegate = self;
        priceField.delegate = self;

}




实现以下三个方法,如果弹出的键盘会遮住输入框 ,整体的界面会向上移动,这样就不会遮住输入框了。自己增加UITextFieldDelegate委托。
只适合iPhone,如果想要支持iPad,只要把216改成iPad上面键盘的高度即可。

- (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 + 32 - (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];                
}


你可能感兴趣的:(iPhone,action,UIView,float,interface,keyboard)