UITextField 用法

来源:互联网 发布:营销软件网站源码 编辑:程序博客网 时间:2024/05/16 10:47

最近接触到UITextField,觉得使用起来还是很有意思,考虑有些新手或没经常用到的同志有这个需要,就直接上自己写的demo,这样直观:

- (void)viewDidLoad

{

    [superviewDidLoad];

    _textField = [[UITextFieldalloc]initWithFrame:CGRectMake(100,50,120,28)];

    _textField.borderStyle =1;//类型

    _textField.autocorrectionType =UITextAutocorrectionTypeNo;//是否开启自动校对功能

    _textField.textColor = [UIColordarkGrayColor];//字体颜色

    _textField.font = [UIFontsystemFontOfSize:16.0f];

    _textField.clearButtonMode =UITextFieldViewModeWhileEditing;//定义清空按钮显示

    _textField.autoresizingMask =UIViewAutoresizingFlexibleWidth;//宽度自适应

    _textField.secureTextEntry =YES;//自动覆盖效果开启

    _textField.delegate =self;

    [self.viewaddSubview:_textField];

    [_textField release];

   

}

#pragma mark textfield 代理

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    // return NO to not change text

   if(strlen([textField.textUTF8String]) >= 10 && range.length !=1)

       return NO;

    

    return YES;

}


-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    

    [textField resignFirstResponder];//收键盘

    return YES;

}

  希望对大家有用!


原创粉丝点击