学习iOS控件之UITextField

学习iOS控件之UITextField

UITextField是一个文本输入框,可以输入字母,数字等。

UITextField的官网地址:https://developer.apple.com/reference/uikit/uitextfield 点击打开链接

    //初始化
    UITextField *textField = [[UITextField alloc] init];
    
    //设置背景色
    textField.backgroundColor = [UIColor redColor];
    
    //设置frame
    textField.frame = CGRectMake(50, 50, 300, 50);
    
    //设置字体大小
    textField.font = [UIFont systemFontOfSize:15];
    
    //设置文本颜色
    textField.textColor = [UIColor blueColor];
    
    //在没有输入的情况,显示的文字
    textField.placeholder = @"请输入内容";
    
    /*
     右边的小叉
     UITextFieldViewModeNever,          从不显示
     UITextFieldViewModeWhileEditing,   编辑是现实(用的最多)
     UITextFieldViewModeUnlessEditing,  编辑时不现实
     UITextFieldViewModeAlways          总是现实
     */
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    /*
    NSTextAlignmentLeft      = 0,    // Visually left aligned   居左对齐

    NSTextAlignmentCenter    = 1,    // Visually centered       居中对齐
    NSTextAlignmentRight     = 2,    // Visually right aligned  居右对齐
    
    NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
    NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script  默认对齐,居左
    */
    //对齐方式
    textField.textAlignment = NSTextAlignmentJustified;
    
    //设置左边视图
    UIButton *overlayButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [overlayButton setImage:[UIImage imageNamed:@"bookmark"] forState:UIControlStateNormal];
    [overlayButton addTarget:self action:@selector(displayBookmarks:)
            forControlEvents:UIControlEventTouchUpInside];
    overlayButton.frame = CGRectMake(0, 0, 28, 28);
    textField.leftView = overlayButton;
    
    /*
     UIKeyboardTypeDefault,                // Default type for the current input method. 默认
     UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters ASCII码
     UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation. 数组和符号
     UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
     UIKeyboardTypeNumberPad,              // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable                          for PIN entry.  数字键盘
     UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers). 电话键盘
     UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number. 和默认一样
     UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
     UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point. 数字和点
     UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
     UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
     UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // A number pad (0-9) that will always be ASCII digits.
     
     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated 废弃
     
     */
    textField.keyboardType = UIKeyboardTypeNumberPad;
    
    [self.view addSubview:textField];


代理的方法:

// return NO to disallow editing.
/**
 是否允许编辑

 @param textField textField description
 @return YES:允许编辑   NO:不想允许编辑
 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}


// became first responder
/**
 开始编辑

 @param textField textField description
 */
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}


// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
/**
 结束编辑

 @param textField textField description
 @return YES:允许结束编辑  NO:不允许借宿编辑
 */
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    return YES;
}


// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
/**
 结束编辑时调用

 @param textField textField description
 */
- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}


// if implemented, called in place of textFieldDidEndEditing:
//textFieldDidEndEditing需要替换
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0){
    
}


// return NO to not change text
/**
 是否改变文本内容

 @param textField textField description
 @param range range description
 @param string string description
 @return YES
 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return YES;
}

// called when clear button pressed. return NO to ignore (no notifications)
/**
 按下clear时调用

 @param textField textField description
 @return YES:调用   NO:不调用
 */
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    return YES;
}

// called when 'return' key pressed. return NO to ignore.
/**
 按下return时调用

 @param textField textField description
 @return YES:调用  NO:不调用
 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值