UIButton及UITextfield

#pragma mark ---UIButton

    UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom] ;
    button.frame =CGRectMake(100, 100, 50, 40);
    button.backgroundColor = [UIColor greenColor];

    //添加背景图片
    //①.设置普通状态下的背景图片
    UIImage *normalImage =[UIImage imageNamed:@"panda.png"];
    [button setBackgroundImage:normalImage forState:UIControlStateNormal];
    //②.设置高亮图片
    UIImage * highlightImage = [UIImage imageNamed:@"111.png"];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    //③.设置按钮的标题
    [button setTitle:@"熊猫" forState:UIControlStateNormal];
    [button setTitle:@"shane" forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

/******添加事件

    [button addTarget:self  action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    [_window addSubview:button];

    [_window makeKeyAndVisible];

    [_window release];

    return YES;
}
//实现点击button的方法
- (void)clickButton:(UIButton *)btn
{
    btn.backgroundColor =[UIColor cyanColor];//此时btn是上面的button对象

   // _window.backgroundColor = [UIColor yellowColor];
}

#pragma mark---UITextfield 文本输入框

    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 30)];
    //textField.backgroundColor =[UIColor grayColor];
    //1.文本的相关属性

    textField .text = @"you are my sunshine";
    textField .borderStyle =UITextBorderStyleRoundedRect;
    textField .textColor =[UIColor redColor];
    textField .textAlignment = NSTextAlignmentCenter;
    textField .placeholder = @"请输入电话号码";//提示占位符

     //2.输入控制相关的属性
    textField .enabled = YES;//是否允许输入,默认为YES
    textField.clearsOnBeginEditing  = YES;//在开始编辑的时候,是否把之前的内容清空,默认为NO;
    textField.secureTextEntry = YES;//是否以密文方式输入
    textField.keyboardType = UIKeyboardTypeURL;//键盘类型
    /*
     typedef enum {

     UIKeyboardTypeDefault,       默认键盘,支持所有字符

     UIKeyboardTypeASCIICapable,  支持ASCII的默认键盘

     UIKeyboardTypeNumbersAndPunctuation,  标准电话键盘,支持+*#字符

     UIKeyboardTypeURL,            URL键盘,支持.com按钮 只支持URL字符

     UIKeyboardTypeNumberPad,              数字键盘

     UIKeyboardTypePhonePad,     电话键盘

     UIKeyboardTypeNamePhonePad,   电话键盘,也支持输入人名

     UIKeyboardTypeEmailAddress,   用于输入电子 邮件地址的键盘

     UIKeyboardTypeDecimalPad,     数字键盘 有数字和小数点

     UIKeyboardTypeTwitter,        优化的键盘,方便输入@、#字符

     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 

     } UIKeyboardType;

     */
    textField.returnKeyType = UIReturnKeyJoin;//return修改

    //自定义键盘视图
    UIView *inputView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 180)];
    inputView.backgroundColor =[UIColor purpleColor];


    UIView *inputAccessaryView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 , 0, 60)];

    inputAccessaryView.backgroundColor = [UIColor yellowColor];
   // textField.inputAccessoryView = inputAccessaryView;

    [inputAccessaryView release];
    //textField.inputView = inputView;
    [inputView release];

    //3.文本输入框外观的相关设置
    textField.borderStyle = UITextBorderStyleRoundedRect;//设置边框样式
    textField.clearButtonMode = UITextFieldViewModeAlways;//设置清除按钮的显示

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 40, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"111.png"] forState:UIControlStateNormal];
    textField.leftViewMode= UITextFieldViewModeAlways;
    textField.leftView = button;

#warning ----设置UITextField的代理
    textField.delegate = self;

    [_window addSubview:textField];
    [textField release];
    [_window makeKeyAndVisible];
    [_window release];

    return YES;
}

#pragma mark ------UITextFieldDelegate方法实现
//将要开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    NSLog(@"%s",__FUNCTION__);//打印当前的方法
    return YES;
}
// return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{

    //[textField becomeFirstResponder];
    NSLog(@"%s",__FUNCTION__);
}// became first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"%s",__FUNCTION__);
    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(@"%s",__FUNCTION__);


}// 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{

    NSLog(@"%s",__FUNCTION__);
    return YES;
}// return NO to not change text

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    NSLog(@"%s",__FUNCTION__);
    return YES;

}// called when clear button pressed. return NO to ignore (no notifications)
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    NSLog(@"%s",__FUNCTION__);
    [textField resignFirstResponder];//取消第一响应者,回收键盘
    return YES;
}// called when 'return' key pressed. return NO to ignore.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值