UITextField 用法

最近接觸到UITextField,覺得使用起來還是很有意思,考慮有些新手或沒經常用到的同志有這個需要,就直接上自己寫的demo,這樣直觀:

- (void)viewDidLoad

{

    [super viewDidLoad];

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

    _textField.borderStyle = 1;//類型

    _textField.autocorrectionType = UITextAutocorrectionTypeNo;//是否開啓自動校對功能

    _textField.textColor = [UIColor darkGrayColor];//字體顏色

    _textField.font = [UIFont systemFontOfSize:16.0f];

    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;//定義清空按鈕顯示

    _textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;//寬度自適應

    _textField.secureTextEntry = YES;//自動覆蓋效果開啓

    _textField.delegate = self;

    [self.view addSubview:_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.text UTF8String]) >= 10 && range.length != 1)

        return NO;

    

    return YES;

}


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

    

    [textField resignFirstResponder];//收鍵盤

    return YES;

}

  希望對大家有用!


發佈了12 篇原創文章 · 獲贊 0 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章