zoukankan      html  css  js  c++  java
  • iOS开篇——UI之UITextField

    创建文本输入框

        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 250, 40)];

    设置边框样式

        textField.borderStyle = UITextBorderStyleRoundedRect;
        /*
         typedef NS_ENUM(NSInteger, UITextBorderStyle) {
         UITextBorderStyleNone, 无效果
         UITextBorderStyleLine, 线性边框 有阴影
         UITextBorderStyleBezel,
         UITextBorderStyleRoundedRect 圆角矩形
         };
         */

    设置text相关

        textField.text = @"2002年的第一场雪";
        //设置字体相关
        textField.font = [UIFont systemFontOfSize:20];
        textField.textColor = [UIColor greenColor];
        //设置自适应
        textField.adjustsFontSizeToFitWidth = YES;
        //设置居中
    //    textField.textAlignment = NSTextAlignmentCenter;

    设置提示语

        //设置提示语
        textField.placeholder = @"请输入";
        
        //设置是否清除 开始编辑时
    //    textField.clearsOnBeginEditing = YES;

    设置/取消第一响应者

        //点击输入框 让输入框成为了第一响应者
        //所谓第一响应者  就是即将编辑的控件  要操作触摸的控件
        [textField becomeFirstResponder];
        //取消成为第一响应者
        [textField resignFirstResponder];

    交互是否开启

        //取消交互是否开启
        textField.userInteractionEnabled = YES;

    设置清除按钮

        //设置清除按钮
        textField.clearButtonMode = UITextFieldViewModeAlways;
        /*
         typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
         UITextFieldViewModeNever,  从不
         UITextFieldViewModeWhileEditing,  编辑时
         UITextFieldViewModeUnlessEditing,  不编辑时显示
         UITextFieldViewModeAlways  一直显示
         };
         */

    设置左右两侧view

        //**********同一个view不能同时设置为左右图片
    //    左侧view
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        view.backgroundColor = [UIColor blueColor];
        textField.leftView = view;
        textField.leftViewMode = UITextFieldViewModeAlways;
    //    右侧view
        UIView * view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
        textField.rightView = view1;
        view1.backgroundColor = [UIColor greenColor];
    //    textField.rightViewMode = UITextFieldViewModeAlways ;

    设置键盘样式

    //设置键盘样式
        textField.keyboardType = UIKeyboardTypeDefault;
        /*
         typedef NS_ENUM(NSInteger, UIKeyboardType) {
         UIKeyboardTypeDefault,                // Default type for the current input method.
         UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
         UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
         UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
         UIKeyboardTypeNumberPad,              // A number pad (0-9). 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).
         
         UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
         
         };
         */
        textField.returnKeyType =UIReturnKeyDone;
        /*
         typedef NS_ENUM(NSInteger, UIReturnKeyType) {
         UIReturnKeyDefault,
         UIReturnKeyGo,
         UIReturnKeyGoogle,
         UIReturnKeyJoin,
         UIReturnKeyNext,
         UIReturnKeyRoute,
         UIReturnKeySearch,
         UIReturnKeySend,
         UIReturnKeyYahoo,
         UIReturnKeyDone,
         UIReturnKeyEmergencyCall,
         UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),
         };
         */

    触摸屏幕  取消第一响应者 退出编辑

    //触摸屏幕 调用此方法
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        UITextField * textField = (id)[self.view viewWithTag:1];
        [textField resignFirstResponder];
    //    [self.view becomeFirstResponder];
    }

    实现UITextFieldDelegate代理方法

    #pragma mark - UITextFieldDelegate
    
    //是否可以开始编辑
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
        return YES;
    }// return NO to disallow editing.
    
    //已经开始编辑
    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        NSLog(@"已经开始编辑");
    }// became first responder
    
    //是否可以结束编辑 yes可以  no不可以
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
        return YES;
    }// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
    
    //已经结束编辑
    - (void)textFieldDidEndEditing:(UITextField *)textField{
        //可在此进行保存草稿
        NSLog(@"已经结束编辑");
    }// may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
    
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        return YES;
    }// return NO to not change text
    
    //触发此方法 返回可否清除
    - (BOOL)textFieldShouldClear:(UITextField *)textField{
        return NO;
    }// called when clear button pressed. return NO to ignore (no notifications)
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
    }
    //点击键盘上的return 执行此方法
    - (BOOL)textFieldShouldReturn:(UITextField *)textField{
        return  YES;
    }// called when 'return' key pressed. return NO to ignore.
  • 相关阅读:
    verilog 数组参数
    跨时钟域设计【一】——Slow to fast clock domain
    跨时钟域设计【二】——Fast to slow clock domain
    跨时钟域设计【三】—— 数据同步
    Vivado学习笔记_002
    使用modelsim仿真DDR3时编译出错的解决方法
    Modelsim仿真tcl脚本与wave.do文件
    %s 与 %0s在 verilog中的区别
    BFM1
    verilog 常用系统函数及例子
  • 原文地址:https://www.cnblogs.com/gwkiOS/p/4990259.html
Copyright © 2011-2022 走看看