zoukankan      html  css  js  c++  java
  • UI初级 TextField

     1 //创建UITextFIeld对象,并设置frame
     2     UITextField *aTextField = [[UITextField alloc] init];
     3     
     4     CGFloat aTextFidldX = 40;
     5     CGFloat aTextFidldY = 44;
     6     CGFloat aTextFidldW = 150;
     7     CGFloat aTextFidldH = 40;
     8     
     9     aTextField.frame = CGRectMake(aTextFidldX, aTextFidldY, aTextFidldW, aTextFidldH);
    10     //背景颜色
    11     aTextField.backgroundColor = [UIColor cyanColor];
    12     
    13     //其他属性
    14     aTextField.text = @"搜索";
    15     aTextField.textColor = [UIColor redColor];
    16     //居中
    17     aTextField.textAlignment = NSTextAlignmentCenter;
    18     //字体大小
    19     aTextField.font = [UIFont systemFontOfSize:20];
    20     //修饰边框
    21     aTextField.borderStyle = UITextBorderStyleRoundedRect;
    22     //占位符
    23     aTextField.placeholder = @"请输入账号";
    24     //编辑时不显示原有的字   干掉了"搜索"
    25     aTextField.clearsOnBeginEditing = YES;
    26     //后面的 x  删除整行tb
    27     aTextField.clearButtonMode = UITextFieldViewModeAlways;
    28     //是否安全输入  输入的字变点
    29     aTextField.secureTextEntry = YES;
    30     //设置键盘的样式
    31 //  aTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    32     //设置ruturn Key的样式
    33 //  aTextField.returnKeyType = UIReturnKeyNext;
    34     
    35 //  键盘回收三部曲
    36     //1. 设置代理对象
    37     aTextField.delegate = self;
    38     //2.  (见 .h 文件)
    39     
    40     //3. 实现协议方法 (见下面)
    41 
    42     UITextField *bTextField = [[UITextField alloc] initWithFrame:CGRectMake(aTextFidldX, aTextFidldY + aTextFidldH + 20, aTextFidldW, aTextFidldH)];
    43     
    44     bTextField.backgroundColor = [UIColor greenColor];
    45     
    46     bTextField.borderStyle = UITextBorderStyleRoundedRect;
    47     
    48     bTextField.placeholder = @"请输入密码";
    49     
    50     bTextField.delegate = self;
    51     
    52     [self.window addSubview:aTextField];
    53     [self.window addSubview:bTextField];
    1 //  3. 实现协议方法
    2 - (BOOL)textFieldShouldReturn:(UITextField *)textField
    3 {
    4     //第一响应者
    5     [textField resignFirstResponder];
    6     return YES;
    7 }

    有代理就得执行协议  我这里是在appdelegate里面写的 你可以在任何一个控制器中写

    // 2. 遵守UITextFieldDelegate协议
    @interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>

    这个协议是在@interface后面写的,就<>里面的  别写错地方

    当然最后这个代理和协议都是为了键盘回收,扩展知识,是在不懂可以不写

  • 相关阅读:
    Extjs Google的Suggest的自动提示 从后台取数据
    vue 使用gojs绘制简单的流程图
    网络流24题の详解
    Codeforces Round #587 (Div. 3) F. WiFi(dp+线段树)
    Codeforces Round #588 (Div. 2) E. Kamil and Making a Stream(dfs)
    Spring使用经验之StandardServletMultipartResolver实现文件上传的基本配置
    MySQL + Amoeba 负载均衡、主从备份方案
    SubVersion(SVN)的安装配置使用
    Tomcat Https配置
    Eclipse 常用快捷键清单
  • 原文地址:https://www.cnblogs.com/wangshuai-1129/p/5284511.html
Copyright © 2011-2022 走看看