进击的UI---------------- UITextField&UIButton

1.UITextField
1⃣️:初始给值
UITextField *textfield1= [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];
textfield1.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
[self.window addSubview:textfield1];
2⃣️:文本设置
①:继承UILabel
(1):textfield1.text = @"123";
(2):textfield1.font = [UIFont systemFontOfSize:30];
(3):textfield1.textColor = [UIColor greenColor];
(4):textfield1.textAlignment = NSTextAlignmentLeft;
②:站位字符串:textfield1.placeholder = @"请输入密码";
3⃣️:输入设置:
(1)打开键盘:textfield1.enabled  = YES;// 默认YES,打开键盘;
(2)清空输入框原有内容:textfield1.clearsOnBeginEditing = YES;// ;默认NO;
(3)安全输入:textfield1.secureTextEntry = YES;// 安全输入;(输入密码);默认是NO;
(4)键盘外貌:textfield1.keyboardAppearance = UIKeyboardAppearanceDefault;
(5)弹出键盘:textfield1.keyboardType = UIKeyboardTypeNumberPad;// 键盘类型;弹出数字键盘
                   textfield1.keyboardType = UIKeyboardTypeEmailAddress;// 弹出带@键盘
                   textfield1.keyboardType = UIKeyboardTypeDefault; // 默认
(6)return键类型:textfield1.returnKeyType = UIReturnKeyDone;// return键类型,done完成;
(7)自定义view代替默认键盘:
一.
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];
    view.backgroundColor = [UIColor redColor];
    textfield1.inputView = view;// 键盘
二.
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];    
    view1.backgroundColor = [UIColor blueColor];
    textfield1.inputAccessoryView = view1;// 键盘上方小键盘
4⃣️:外观设置:
①输入框类型
(1):textfield1.borderStyle = UITextBorderStyleRoundedRect;// 输入框:圆角矩形;
(2):textfield1.borderStyle = UITextBorderStyleNone;
(3):textfield1.borderStyle = UITextBorderStyleLine;
(4):textfield1.borderStyle = UITextBorderStyleBezel;
②显示清除按钮:
(1):textfield1.clearButtonMode = UITextFieldViewModeAlways;
(2):textfield1.clearButtonMode = UITextFieldViewModeNever;
③左右视图
(1):
UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
view2.backgroundColor = [UIColor blackColor];
textfield1.leftView = view2;
textfield1.leftViewMode = UITextFieldViewModeAlways;
(2):
    textfield1.rightView = view2;
    textfield1.rightViewMode = UITextFieldViewModeAlways;
    [textfield1 release];
    textfield1 = nil;
2.UIButton
1⃣️:初始化给大小:便利构造器:
 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = CGRectMake(100, 100, 100, 100);
//    button.backgroundColor = [UIColor redColor];
//    [self.window addSubview:button];
2⃣️:添加点击事件
(1):[button setTitle:@"你点我啊" forState:UIControlStateNormal];// 设置button标题
(2):
[button addTarget:self action:@selector(buttonAction)
forControlEvents:UIControlEventTouchUpInside];// 点击事件(添加方法)
 
 实现方法选择器里面的button响应事件
- (void)buttonAction{
    NSLog(@"你打我啊");
}
3⃣️:添加背景图片:
[button setBackgroundImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
 self.flag = YES;
(1):@property (nonatomic,assign)BOOL flag;
(2):
 添加点击事件的方法,传入一个参数,参数是这个事件的响应者,参数的类型,是响应者的类型;
- (void)buttonAction:(UIButton *)sender{
    if (self.flag == YES) {
        [sender setBackgroundImage:[UIImage imageNamed:@"22.png"] forState:UIControlStateNormal];
        self.flag = NO;
    }else{
        [sender setBackgroundImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
        self.flag = YES;}}
4⃣️:外观控制
①:前景色图片
 注意:1 覆盖标题 2 不会随着buttonframe进行拉伸 3 前景图片必须是镂空图(好像只有线条的图片)
(1):设置前景色图片:[button1 setImage:[UIImage imageNamed:@"55.png"] forState:UIControlStateNormal];
( 2 ):获取前景色图片:UIImage *image1 = [button1 imageForState:UIControlStateNormal];
②:背景色图片
(1):设置背景色图片:[button1 setBackgroundImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
( 2 ):获取背景色图片:UIImage *image2 = [button1 backgroundImageForState:UIControlStateNormal];
③:标题
(1):设置标题:[button1 setTitle:@"点我啊" forState:UIControlStateNormal];
( 2 ):获取标题: NSString *title = [button1 titleForState:UIControlStateNormal];
( 3 ):标题颜色:[button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
④:阴影:[button1 setTitleShadowColor:[UIColor orangeColor] forState:UIControlStateNormal];
⑤:获取颜色:UIColor *color1 = [button1 titleColorForState:UIControlStateNormal];
3.delegate
1⃣️:三部步骤
1,遵循协议,找到当前类(到AppDelegate.h文件中的尖括号+,UITextFieldDelegate)
2,设置代理 操作谁,谁.delegate = self;
3,实现代理方法
2⃣️:三部代码:
①:@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>
②:
UITextField *text1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    text1.backgroundColor = [UIColor grayColor];
    [self.window addSubview:text1];
    text1.returnKeyType = UIReturnKeyDone;
text1.delegate = self;
[text1 release];
    text1 = nil;
③:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];// 释放第一响应者
    return YES;}
    // 实现代理方法注意:
    //     1有返回值
    //     2释放第一响应者
4.程序启动流程
1⃣️:
程序的启动流程
// 首先 main函数(作为程序入口)
//  1 创建应⽤用程序(UIApplication)实例
//  2 创建应⽤用程序代理(Application)实例 不能够把所有的代码写在MAIN函数里 可以写代码在Application里 体现封装的特性
//  3 建⽴立事件循环(runloop:死循环,不断检测程序运⾏行状态,是否被触摸、晃动等)
2⃣️:
①:
- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"程序将要退出活跃状态");
}
②:
- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"程序已经进入后台");
}
③:
- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"程序将要进入前台");
}
④:
- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"程序已经变成活跃状态");
}
⑤:
- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"程序将要终结"); 
}
⑥:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"已经完成启动");
}
原文地址:https://www.cnblogs.com/sharkHZ/p/4984140.html