iOS学习笔记(二)————基本控件

/ 0评 / 0

iOS的开发主要集中在前端的展示,所以第一步就是要学习怎样展示,在iOS的SDK中有许许多多的控件以供我们使用,可以先看一下这些iOS中的继承图:
UIjicheng

UIjichengtu

iOS中控件几乎都是UI开头的,他们属于UIKit这个包,先看一下最常用的一些控件的用法
一、标签:就是我们看到的显示文字的控件

- (void)testLabel{
    //    控件
    //    标签
    //    1.创建对象
    //    UI  user interface
    UILabel *label = [[UILabel alloc] init];
    
    //    frame框架
    //2.设置位置以及大小
    //    包括4个参数  x,y,width height
    label.frame = CGRectMake(50, 50, 100, 50);
    
    //  设置文本内容
    label.text = @"你好 阿萨德发上来就发生了对方就撒到了三大纪律发 ";
    
    //    文本颜色
    //    需要一个UIColor的对象
    label.textColor = [UIColor redColor];
    //    [label setTextColor:[UIColor redColor]]
    
    // 设置背景颜色
    label.backgroundColor = [UIColor yellowColor];
    
    //    设置字体大小
    
    //   获取所有字体的家族
    //    NSArray *array = [UIFont familyNames];
    //
    ////    遍历
    //    for (NSString *string in array)
    //    {
    //        NSLog(@"string ===== %@",string);
    //
    //        NSArray *array1 = [UIFont fontNamesForFamilyName:string];
    //
    //        for (NSString *name in array1)
    //        {
    //            NSLog(@"----- >>> %@",name);
    //        }
    //    }
    //    label.font  = [UIFont fontWithName:@"GillSans-Italic" size:20];
    
    label.font = [UIFont systemFontOfSize:20];
    
    //    对齐样式
    label.textAlignment = NSTextAlignmentCenter;
    //    设置断线的样式
    label.lineBreakMode = NSLineBreakByTruncatingMiddle;
    
    
    //    add 添加  Subview 子视图
    //   3. 添加子视图
    
    //    因为 UILabel 继承uiview 所有label 也是uiview的对象
    [self.view addSubview:label];
}

二、输入框

- (void)testTextField{
    //    2. 输入框
    UITextField *textField = [[UITextField alloc] init];
    
    //    设置边框
    //    border 边角 Rounded 圆  rect 矩形
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.backgroundColor = [UIColor yellowColor];
    
    //    secure  安全的  Text 文本  Entry  输入
    textField.secureTextEntry = YES;
    //    clear 清除
    //    Button 按钮
    
    textField.clearButtonMode = UITextFieldViewModeUnlessEditing;
    
    //    提示
    //    place 地点  holder  持有者
    textField.placeholder = @"请输入密码";
    
    textField.frame = CGRectMake(50, 200, 100, 50);
    
    [self.view addSubview:textField];
}

三、按钮

- (void)testButton{
    //    按钮
    //    UIButton *button = [[UIButton alloc] init];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    button.frame = CGRectMake(50, 300, 100, 50);
    
    //   设置标题
    [button setTitle:@"按钮-1" forState:UIControlStateNormal];
    //    设置高亮状态
    [button setTitle:@"按钮-Highlighted" forState:UIControlStateHighlighted];
    //    设置按钮字体的颜色
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    button.backgroundColor = [UIColor redColor];
    
    //    绑定方法
    //    Target  目标  self
    //    action  动作 <#(SEL)#> @selector(方法的名字)
    //forControlEvents  控制事件  TouchUpInside 点击松开
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
}

//点击按钮时会调用次方法
- (void)buttonClick{
    NSLog(@"被调用了");
}

四、图片显示


- (void)testImageView{
    //    图片视图
    //    [UIScreen mainScreen].bounds   全屏大小  0.0.320.480
    //    [UIScreen mainScreen].applicationFrame   全屏大小  0.20.320.460
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    
    //    指定显示图片
    //    除了后缀名png的可以省略,其他的不行
    imageView.image = [UIImage imageNamed:@"1.jpg"];
    
    //    用户可交互性
    imageView.userInteractionEnabled = YES;
    
    [self.view addSubview:imageView];
    //    NSStringFromCGRect 用来输出坐标
    NSLog(@"====== %@",NSStringFromCGRect([UIScreen mainScreen].bounds));
    NSLog(@"---->>> %@",NSStringFromCGRect([UIScreen mainScreen].applicationFrame));
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame= CGRectMake(0, 0, 100, 100);
    
    btn.tag = 100;
    
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    
    btn.backgroundColor = [UIColor blackColor];
    
    //绑定方法传值
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    btn.backgroundColor = [UIColor greenColor];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:50];
    
    [imageView addSubview:btn];
    
}

- (void)btnClick:(UIButton *)sentBtn{
    NSLog(@"你觉的能点击吗?");
    NSLog(@"======>>>> %ld",sentBtn.tag);
}

五、视图view,iOS中的控件差不多都是继承于view

- (void)testView{
    //    大部分的UI控件都 继承 UIView
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
    
    view.backgroundColor = [UIColor yellowColor];
    
    [self.view addSubview:view];
    
    
    // 子视图
    UITextField *textField = [[UITextField alloc] init];
    textField.bounds = CGRectMake(0, 0, 100, 100);
    textField.center = CGPointMake(100, 100);
    
    NSLog(@"---->>> %@",NSStringFromCGPoint(textField.center));
    
    textField.borderStyle = UITextBorderStyleRoundedRect;
    
    [view addSubview:textField];
}

六、根据tag值获得视图上的控件

//找对象,每一个控件都有一个tag值,可以根据这个tag值找到这个对象
- (void)testGetObject{
    //    局部变量的作用范围是当前大括号
    //    全局变量作用范围是整个类
    
    lab = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
    lab.text = @"你好=====";
    [self.view addSubview:lab];
    
    
    UILabel *aLab = [[UILabel alloc] initWithFrame:CGRectMake(150, 50, 100, 50)];
    
    //    tag 是uiview的属性,所有的继承于uiview的类都可以使用tag属性
    //    tag  标签
    aLab.tag = 10086;
    
    aLab.text = @"hello";
    [self.view addSubview:aLab];
    
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(50, 200, 100, 50);
    
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor blackColor];
    
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
}

//找对象
//1.全局变量  (记得要把局部的指针删除)
//2.设置tag值  通过viewWithTag:方法来获取对象 (记得强制类型转换)

- (void)btnClick{
    NSLog(@"========");
    
    //    有了对象就可以取操作属性了
    lab.frame = CGRectMake(100, 300, 50, 50);
    
    
    //    tag  设置tag值
    //    viewWithTag   属于UIView的方法
    UILabel *label = (UILabel *)[self.view viewWithTag:10086];
    
    //   就可以去改变对象的属性
    label.numberOfLines = 0;
    label.backgroundColor = [UIColor yellowColor];
}

代码请查看 http://git.oschina.net/zcb1603999/LearningiOS

评论已关闭。