初始UI(UILabel、UIButton、UITextField)

UI指的是UserInterFace(用户界面)

UILabel

    //获取屏幕的尺寸
    UIScreen *screen = [UIScreen mainScreen];
    self.window = [[UIWindow alloc]initWithFrame:screen.bounds];
    self.window.backgroundColor = [UIColor redColor];
    [self.window makeKeyAndVisible];
//    self.window.rootViewController
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
    label.text = @"You have to have the courage to examine who you really are, to come to terms with the dark corner of your own soul.";
    label.textColor = [UIColor whiteColor];
    //字体大小
//    label.font = [UIFont fontWithName:@"Telugu Sangam MN" size:28];
//    label.font = [UIFont boldSystemFontOfSize:28];//加粗
    label.font = [UIFont systemFontOfSize:20];
    NSArray *array = [UIFont familyNames];
    NSLog(@"%@",array);
    //对齐方式
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor blackColor];
    //文本换行
    label.numberOfLines = 0;//0代表不限制label行数
    //省略号放置地方,并没什么卵用
    label.lineBreakMode = NSLineBreakByTruncatingHead;//省略号放置中间
    [self.window addSubview:label];

    //根据文本内容的内容的长度确定label的宽度
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectZero];
    NSString *text = @"黑凤梨";
    label1.text = text;
    label1.font = [UIFont systemFontOfSize:28];
    CGSize size = [text sizeWithAttributes:@{NSFontAttributeName:label1.font}];
    label1.frame = CGRectMake(100, 300, size.width, size.height);
    label1.backgroundColor = [UIColor blackColor];
    label1.textColor = [UIColor whiteColor];
    [self.window addSubview:label1];

    CGRect rect = [label.text boundingRectWithSize:CGSizeMake(screen.bounds.size.width, 300) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];
    label.frame = CGRectMake(0, 20, rect.size.width, rect.size.height);

UIBotton

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];
    [self.window makeKeyAndVisible];
    //凡是继承于UIControl的空间都具有响应事件的能力
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 60, 60, 60);
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"我是按钮" forState:UIControlStateNormal];
    [button setTitle:@"高亮状态" forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    button.backgroundColor = [UIColor blackColor];
    [self.window addSubview:button];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(10, 130, 60, 60);
    [self.window addSubview:button1];
    button1.backgroundColor = [UIColor blackColor];
    [button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
    [button1 setTitle:@"正常状态" forState:UIControlStateNormal];
    [button1 setTitle:@"选中状态" forState:UIControlStateSelected];
    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(10, 200, 60, 60);
    [self.window addSubview:button2];
    button2.backgroundColor = [UIColor blackColor];
    [button2 addTarget:self action:@selector(button2Action:) forControlEvents:UIControlEventTouchUpInside];
    [button2 setTitle:@"正常状态" forState:UIControlStateNormal];
    [button2 setTitle:@"编辑状态" forState:UIControlStateDisabled];
    [button2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    button2.enabled = NO;



    //--------------------------------------------------
    //自定义按钮
    UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cButton.frame = CGRectMake(100, 60, 60, 60);
    [cButton addTarget:self action:@selector(cButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [cButton setImage:[UIImage imageNamed:@"11"] forState:UIControlStateNormal];
    [cButton setImage:[UIImage imageNamed:@"123.jpg"] forState:UIControlStateHighlighted];
    [self.window addSubview:cButton];

    UIButton *cButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    cButton1.frame = CGRectMake(100, 130, 60, 60);
    [cButton1 addTarget:self action:@selector(cButton1Action:) forControlEvents:UIControlEventTouchUpInside];
    [cButton1 setImage:[UIImage imageNamed:@"11"] forState:UIControlStateNormal];
    [cButton1 setImage:[UIImage imageNamed:@"123.jpg"] forState:UIControlStateSelected];
    [self.window addSubview:cButton1];
    //------------------------------------
    //长按
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(40, 300, 300, 300)];
    view.backgroundColor = [UIColor blackColor];
    [self.window addSubview:view];
    UIButton *kButton = [UIButton buttonWithType:UIButtonTypeSystem];
    kButton.frame = CGRectMake(120, 135, 60, 30);
    [kButton setTitle:@"按住说话" forState:UIControlStateNormal];
    [kButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [kButton addTarget:self action:@selector(kButtonActionTouchDown:) forControlEvents:UIControlEventTouchDown];
    [kButton addTarget:self action:@selector(kButtonActionTouchUpInserd:) forControlEvents:UIControlEventTouchUpInside];

    [view addSubview:kButton];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"11"]];
    imageView.frame = CGRectMake(125, 55, 50, 70);
    [view addSubview:imageView];

    flagView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"123.jpg"]];
    flagView.frame = CGRectMake(177, 55, 20, 20);
    flagView.hidden = YES;
    flagView.animationImages = @[[UIImage imageNamed:@"123.jpg"],[UIImage imageNamed:@"11"]];
    flagView.animationDuration = 0.05;
    [view addSubview:flagView];

    return YES;
}


-(void)buttonAction:(UIButton *)sender{
    NSLog(@"button响应事件");
}
-(void)button1Action:(UIButton *)sender{
    NSLog(@"button1响应事件");
    sender.selected = !sender.selected;
}
-(void)button2Action:(UIButton *)sender{
    NSLog(@"button2响应事件");
}
-(void)cButtonAction:(UIButton *)sender{
    NSLog(@"cButton响应事件");
}
-(void)cButton1Action:(UIButton *)sender{
    NSLog(@"cButton1响应事件");
    sender.selected = !sender.selected;
}
-(void)kButtonActionTouchDown:(UIButton *)sender{
    NSLog(@"按下");
    [flagView setHidden:NO];
    [flagView startAnimating];
}
-(void)kButtonActionTouchUpInserd:(UIButton *)sender{
    [flagView setHidden:YES];
    [flagView stopAnimating];
    NSLog(@"松开");
}

UITextField

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];
    //UITextField文本框
    textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 60, 200, 30)];
    [self.window addSubview:textField];
    //在实际开发中不用borderStyle属性(一般)
//    textField.borderStyle = UITextBorderStyleLine;
    textField.background = [UIImage imageNamed:@"11"];
    textField.leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"22"]];
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.placeholder = @"请输入密码";
    textField.secureTextEntry = NO;//掩码

    //键盘是作为文本框的第一响应者
    //让键盘自动弹出
//    [textField becomeFirstResponder];
    //键盘回收,即注销第一响应者 [textField resignFirstResponder];

    //修改键盘样式
//    textField.keyboardType = UIKeyboardTypeNumberPad;
//    textField.returnKeyType = UIReturnKeySearch;

    //文本框的重要属性 delegate
    textField.delegate = self;



    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(60, 100, 60, 44);
    [button setTitle:@"登陆" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button];
    [self.window makeKeyAndVisible];
    return YES;
}
-(void)loginAction:(UIButton *)sender{
    NSLog(@"登陆,文本框的内容为:%@",textField.text);
    //注销第一响应者,回收键盘
    [textField resignFirstResponder];
}
#pragma mark ----------------------------------------------
//手放在文本框上就执行这个方法,如果返回NO键盘不能弹出
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}
//用户开始编辑时候执行
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"%s",__func__);
}
//编辑完之后执行的方法
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    NSLog(@"%s",__func__);
    return YES;
}
//执行完之后的方法
- (void)textFieldDidEndEditing:(UITextField *)textField{
    NSLog(@"%s",__func__);
}
//编辑过程中
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//    NSLog(@"%@",range);
    NSLog(@"%@",string);
    NSLog(@"%s",__func__);
    return YES;
}
//按clear按钮之后执行的行为
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    NSLog(@"%s",__func__);
    return YES;
}
//按return之后执行
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [textField resignFirstResponder];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值