UITextField 的使用

直接上代码:


   /*
        UITextField  的使用
     */

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];

    //borderStyle属性用来控制输入框的外观,一共有四种:无样式(默认),线框,刀角,圆角矩形。
    textField.borderStyle = UITextBorderStyleRoundedRect;
    //placeholder是 占位字符串,用于提示用户该输入框可以输入什么内容。
    textField.placeholder = @"请输入账号";
    //为文本框 添加编辑的内容
//    textField.text = @"12314564654";
    //开始编辑时 清空已有内容
    textField.clearsOnBeginEditing = YES;
    //对输入的内容以 密码 形式显示 “*”,默认的为 NO 。
//    textField.secureTextEntry = YES;
    //通过设置清除按钮的模式来控制右侧的清除按钮的显示,默认不显示,特别注意:unlessEditing表示除了编辑状态外。
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    //设置键盘的类型,常用的URL,EmailAddress, NumberPad, Default。
    textField.keyboardType = UIKeyboardAppearanceDefault;
    //设置键盘样式,两种样式,浅色和深色,默认为浅色。
    textField.keyboardAppearance = UIKeyboardAppearanceDark;

    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 40)];
    aView.backgroundColor = [UIColor grayColor];
//    aView.text = @"我的键盘";
//    aView.textAlignment = NSTextAlignmentCenter;
    //为输入框添加输入辅助视图

    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeSystem];
    doneButton.frame = CGRectMake(self.window.bounds.size.width - 50, 0, 50, 40);
    [doneButton setTitle:@"完成" forState:UIControlStateNormal];
    doneButton.backgroundColor = [UIColor lightGrayColor];
    [doneButton addTarget:self action:@selector(handleDoneButtonAction:) forControlEvents:UIControlEventTouchDown];
    [aView addSubview:doneButton];

    textField.inputAccessoryView = aView;
    [aView release];

    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 220)];
    inputView.backgroundColor = [UIColor lightGrayColor];
    textField.inputView = inputView;
    [inputView release];

//自定义 键盘

    NSArray *butArray = @[@[@"1",@"2",@"3"],@[@"4",@"5",@"6"],@[@"7",@"8",@"9"],@[@"__",@"0",@"<-"]];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
            but.frame = CGRectMake(inputView.bounds.size.width / 3 * j + 1, inputView.bounds.size.height / 4 *  i + 1, inputView.bounds.size.width / 3 - 2, inputView.bounds.size.height / 4 - 2);
            but.backgroundColor = [UIColor grayColor];
            but.showsTouchWhenHighlighted = YES;
            [but setTitle:butArray[i][j] forState:UIControlStateNormal];
            [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [but addTarget:self action:@selector(butClickEvent:) forControlEvents:UIControlEventTouchDown];
            [inputView addSubview:but];
        }
    }

    //设置系统键盘右下角返回键的类型。
    textField.returnKeyType = UIReturnKeyDone;
    textField.tag = 100;
    textField.center = self.window.center;
    //为textField指定代理对象。
    textField.delegate = self;

    [self.window addSubview:textField];
    [textField release] ;




- (void)butClickEvent:(UIButton *)sender {
//    [sender titleLabel];
    UITextField * temp = (UITextField *)[self.window viewWithTag:100];
    temp.text = [temp.text stringByAppendingString:sender.titleLabel.text];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值