首页 > 其他 > 详细

UITextField 的使用

时间:2015-07-14 11:46:34      阅读:78      评论:0      收藏:0      [点我收藏+]

直接上代码:


   /*
        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];
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UITextField 的使用

原文:http://blog.csdn.net/zhengang007/article/details/46873133

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!