UI-UITextField UIButton

1. UITextField:

UITextField的属性:(部分)

文本显示:

1. 有UILable的一堆属性:text font textColor textAlignment

2. placeholder:占位字符串

输入控制:

3. enabled:是否允许在输入框内输入东西

4. secureTextEntry:密文输入

5. keyboardType:弹出键盘的样式

6. returnKeyType:return键的样式

7. inputView:自定义  输入的视图  默认是键盘

8. inputAccessoryView:输入视图上方的辅助视图

外观控制:

9. borderStyle:边框样式

10. clearButtonMode:清除按钮(就是后面那个小叉)

11. leftView:输入框的左视图

12. leftViewMode:输入框的左视图的显示模式


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    
    UIView *view = [[UIView alloc] initWithFrame:_window.bounds];
    [_window addSubview:view];
    [view release];
    
    
#pragma UITextField:输入框 
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 200, 30)];
    [view addSubview:textField];
    
// 属性enabled:YES能输入   NO不能输入内容
    textField.enabled = YES;
    
//    textField.text = @"haha"; // 属性text:默认的文本   例如:记住密码 记住账号
 
// 属性placeholder:占位文本  (提示语)
    textField.placeholder = @"请输入用户名~ ~ ";
    
// 属性:borderStyle textField的边框样式
    textField.borderStyle = UITextBorderStyleRoundedRect;
    
// 属性:clearButtonMode,textField的清空按钮
    textField.clearButtonMode = UITextFieldViewModeAlways;
    
    textField.secureTextEntry = YES; //  密文输入
    
// 属性:keyboardType 控制弹出的键盘的样式
    textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;// 这个是数字和标点符号
    
// return键的样式(return键长得是什么样子)
    textField.returnKeyType = UIReturnKeyYahoo;
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftView.backgroundColor = [UIColor orangeColor];
// leftView左视图  一定要设置leftViewMode才能显示出来  也有leftRight这个属性
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];
    inputView.backgroundColor = [UIColor yellowColor];
// 键盘上方的辅助视图
    textField.inputAccessoryView = inputView;
    
    UIView *inView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    inView.backgroundColor =[UIColor purpleColor];
    
// 把键盘替换了  inputView就是键盘
    textField.inputView = inView;
    
    
#pragma delegate:遵守协议--设置代理--实现方法
// 回收键盘之----设置代理 需要遵守协议:UITextFieldDelegate
    textField.delegate = self;

    [textField release];
    [leftView release];
    [inputView release];
    [inView release];
    return YES;
}
// 回收键盘之----实现方法:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    // 释放第一响应者,回收键盘
    [textField resignFirstResponder];
    return YES;
}

2. UIButton:

1. 按钮不需要释放,使用的是类方法创建的button

2. addTarget: action: forControlEvents: 添加事件 执行action方法

3. removeTarget: action: forControlEvents: 移除按钮的点击事件

4. setTitle: forState     setTitleColor: forState

5. button1.titleLabel.font = [UIFont systemFontOfSize:20];   改变button的字体大小

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    
    UIView *backView = [[UIView alloc] initWithFrame:_window.bounds];
    backView.backgroundColor = [UIColor whiteColor];
    [_window addSubview:backView];
#pragma UIButton:初始化的时候有点不一样
    
    // 初始化的时候和View不一样了~ ~ ~
    _button = [UIButton buttonWithType:UIButtonTypeSystem];// 便利构造器  不用释放
    _button.frame = CGRectMake(50, 50, 100, 50);
    _button.backgroundColor = [UIColor cyanColor];
    [backView addSubview:_button];

// 设置抬头,没有.text  button是集合控件 一般都是Normal
    [_button setTitle:@"BUT" forState:UIControlStateNormal];
    
//    [_button setTitle:@"aaaa" forState:UIControlStateHighlighted]; // 长按才会显示
    
//    [_button setTitle:@"什么鬼" forState:UIControlStateSelected];
//    _button.selected = YES;// selected是button的属性
    
// 设置圆角 如果值等于button(正方形的时候)的宽一半 就是个圆
    _button.layer.cornerRadius = 25;
    
//边框的颜色
    _button.layer.borderColor = [UIColor blackColor].CGColor;
    
// 边框的粗细
    _button.layer.borderWidth = 1.0;
    
    
#pragma  修改button背景色的方法:
    
//    修改button背景色的方法一
//    _button.tag = 101;
//    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    // 修改button背景色的方法二
    [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 修改button背景色的方法三
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];// 给button添加事件的
        
    //  设置button抬头颜色
    [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal]; // 设置阴影颜色 然并卵
    [backView release];
    return YES;
}

#pragma button的响应事件:
//  方法一:利用tag
- (void)buttonAction{
    [((UIView *)_window.subviews[0]) viewWithTag:101].backgroundColor = [UIColor greenColor];
}

  方法二:把button设置为属性
//- (void)buttonAction{
//    _button.backgroundColor = [UIColor redColor];
//}
// 方法三:传参数,谁调用这个方法,传过来的就是谁
- (void)buttonAction:(UIButton *)button{  // 这个方法  点击一下就可以换一个随机颜色
    CGFloat num1 = arc4random()%256;
    CGFloat num2 = arc4random()%256;
    CGFloat num3 = arc4random()%256;
    button.backgroundColor = [UIColor colorWithRed:num1/255.0 green:num3/255.0 blue:num2/255.0 alpha:1.0];
}

总结:





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值