iOS开发 一些基础控件知识

#pragma mark    UIView 视图
/*
     UIView是一个矩形的视图,用来展示所有的东西。
     1.在iOS中与所有可见的控件都是UIView的子类或衍生类
     使用步骤:1.创建对象 2.设置属性 3.扔到窗户上

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    //颜色
    view.backgroundColor = [UIColor yellowColor];
    //将view放在UIView上
    [self.view addSubview:view];
    //贴图片
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"5.jpg"]];
    imageView.frame = view.frame;
    [self.view addSubview:imageView];
   
 
//frame
 
     frame 就是视图显示的大小和位置,
     他是一个结构体,类型是CGRect
     frame:控制位置和大小
     center:控制位置(中心点)代表的是自身的坐标系
     bounds:控制大小(以自己的左上角为原点)
     view.center = self.view.center;//居中
 
//UIView 的属性
   //1.设置是否隐藏
     view.hidden = NO;
     //2.设置透明度:视图的透明度设置是会传递到子视图中的
     view.alpha = 0.4;
     //3.tag值:给这个对象一个标记号,后面可以根君这个标记号取到这个视图
     view.tag = 119;
     
     UIView *v1 =  [self.view viewWithTag:119];
     v1.backgroundColor = [UIColor redColor];
     //4.superView-subviews
     //superView:改变Window的颜色,(取到父视图的对象)
     view.superview.backgroundColor = [UIColor orangeColor];
     //5.subViews:拿到所有的子视图
     NSArray *arr = self.view.subviews;
     for (UIView *vi in arr) {
     vi.backgroundColor = [UIColor greenColor];
     }
     //6.设置圆角
     view.layer.masksToBounds = YES;//设置是否支持圆角
     view.layer.cornerRadius = 50;//设置圆角的半径
     
 

    //1.
     UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 150, 200)];
     blueView.backgroundColor = [UIColor blueColor];
     [self.view addSubview:blueView];
     
     UIView *redView = [[UIView alloc]initWithFrame:CGRectMake(70, 30, 260, 150)];
     redView.backgroundColor =[UIColor redColor];
     [self.view addSubview:redView];
     
     UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 150, 100)];
     greenView.backgroundColor = [UIColor greenColor];
     //添加视图
     //[self.view addSubview:greenView];
     //插入视图
     // [self.view insertSubview:greenView aboveSubview:blueView];//把蓝的贴在绿的上
     //[self.view insertSubview:greenView belowSubview:blueView];
     //从里到外,从0开始逐渐增加
     [self.view insertSubview:greenView atIndex:0];
     //把某一个子视图带到最前面
     [self.view bringSubviewToFront:blueView];
     //把某一个子视图放在最后面
     [self.view sendSubviewToBack:blueView];
     //把两个视图交换位置
     [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:2];
     //将某个视图从父视图移除出去
     [redView removeFromSuperview];
     */

#pragma mark    UILabel标签
/*
    //使用步骤
    //1.创建对象
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 300, 500)];
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 50)];
    //2.设置属性
    label.text = @"hello,hi";
    //3.颜色
    label.backgroundColor = [UIColor blueColor];
    //4.添加视图
    [self.view addSubview:label];
    //设置圆角
    label.layer.masksToBounds = YES;//设置是否支持圆角
    label.layer.cornerRadius = 50;//设置圆角的半径
 
    //文字颜色
    label.textColor = [UIColor orangeColor];
    //字体(大小)注意字体名不能写错
    //label.font = [UIFont fontWithName:@"Hiragino Sans" size:25];
    //label.font = [UIFont systemFontOfSize:40];
    //    NSArray *array = [UIFont familyNames];
    //    for (NSString *name in array) {
    //        NSLog(@"%@",name);
    //    }
    //    //设置字体大小
    //    label.font = [UIFont systemFontOfSize:30];
        //设置对齐方式(默认左对齐)文字居中
    //    label.textAlignment = NSTextAlignmentCenter;
        label.textAlignment = NSTextAlignmentLeft;//左对齐
        label.textAlignment = NSTextAlignmentRight;//右对齐
    //    //设置文字阴影
    //    label.shadowColor = [UIColor grayColor];
    //    //设置阴影的偏移距离(5,10)代表的是x,y的偏移距离
    //    label.shadowOffset = CGSizeMake(5, 10);
    label.text = @"what are you doing,Im doing dadaima.ren sheng ru ci jian nan";
    //设置label的显示行数,设置为0的时候自动换行
    label.numberOfLines = 0;
    //设置换行模式
    label.lineBreakMode = NSLineBreakByCharWrapping;
    
    //适配
        CGFloat x = CGRectGetMinX(view1.frame);
        CGFloat y = CGRectGetMaxY(view1.frame)+20;
        CGFloat width = CGRectGetWidth(view1.frame);
        UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(x,y, width, 80)];
        view2.backgroundColor = [UIColor grayColor];
        [self.view addSubview:view2];
    
*/

#pragma mark    UITextField文本框
/*  // UITextField(文本输入框)
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
    tf.backgroundColor = [UIColor whiteColor];
    //边框样式
    tf.borderStyle = UITextBorderStyleRoundedRect;
    //添加视图
    [self.view addSubview:tf];
    //强制推出或者回收键盘(commend+k);
    //输入框的属性设置:分三类1.文本控制 2.输入控制 3.外观配置
#pragma mark          1.文本控制
  //设置输入框的提示文字
     tf.placeholder = @"小明是追风追风少年";
     //设置输入框的文字颜色
     tf.textColor = [UIColor redColor];
     //设置文字大小
     tf.font = [UIFont systemFontOfSize:18];
     //文本框的文字
     tf.text = @"那是他逝去的青春";
     tf.textAlignment = NSTextAlignmentRight;
   
#pragma mark          2.输入控制
    //tf.text = @"检测运行";
    //设置是否可以输入
    //tf.enabled = YES;
    //    //设置数字键盘
    //    tf.keyboardType = UIKeyboardTypeNumberPad;
    //    //表情
    //    tf.keyboardType = UIKeyboardTypeNamePhonePad;
    //    //符号
    //    tf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    //设置返回键的样式
    //tf.returnKeyType = UIReturnKeyJoin;
    //tf.returnKeyType = UIReturnKeyGo;
    //tf.returnKeyType = UIReturnKeyNext;
    //是否在输入的时清空
    //tf.clearsOnBeginEditing = YES;
    //设置文本为匿名的方式
    //tf.secureTextEntry = YES;
    //设置辅助视图
    //    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    //    view1.backgroundColor = [UIColor cyanColor];
    //    tf.inputAccessoryView = view1;
    //
    //    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
    //    view2.backgroundColor = [UIColor orangeColor];
    //    tf.inputView = view2;
    
#pragma mark          3.外观控制
     //设置边框样式
     //tf.borderStyle = UITextBorderStyleRoundedRect;
     //清空按钮
     tf.text = @"专治各种不服";
     tf.clearButtonMode = UITextFieldViewModeAlways;
     //设置输入框的视图
     UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
     view3.backgroundColor = [UIColor cyanColor];
     tf.leftView = view3;
     tf.leftViewMode = UITextFieldViewModeAlways;
     
     UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
     view4.backgroundColor = [UIColor cyanColor];
     tf.rightView = view4;
     tf.rightViewMode = UITextFieldViewModeAlways;
     
     //    UIImageView *imageView = [[UIImageView alloc]init];
     //    imageView.image = [UIImage imageNamed:@"1.jpg"];
     //
     //    imageView.contentMode = UIViewContentModeScaleToFill;
     //    [view addSubview:imageView];
     
     */
 
#pragma mark    UIButton按钮
/* //button 按钮,单纯的处理用户的点击响应
    //创建对象
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    //设置属性
    button.frame = CGRectMake(100, 200, 150, 50);
   
  [button setTitle:@"点我啊" forState:UIControlStateNormal];
    [button setTitle:@"你还真点啊" forState:UIControlStateHighlighted];
    //
    [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor cyanColor].CGColor;
   button.backgroundColor = [UIColor orangeColor];
    //    //设置标题的颜色
    //    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //    //获取制定状态下的标题
    //    NSString *str1 = [button1 titleForState:UIControlStateNormal];
    //    //获取制定状态下的颜色
    //    UIColor *color1 = [button1 titleColorForState:UIControlStateNormal];
    //
    //    [button1 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    //    //改字体
    //    button1.titleLabel.font = [UIFont systemFontOfSize:40];
    //
    //    //设置button的背景图片。
    //    [button1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    //
    //
    //    //获取指定状态下的背景图片
    //    UIImage *tempImage = [button1 imageForState:UIControlStateNormal];
    //
    //    //设置前景图片 必须是镂空图,或者是线条勾勒的图片
    //    [button1 setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

    //设置button的图片
//    UIImage *image = [UIImage imageNamed:@"1.jpg"];
//    [button setBackgroundImage:image forState:UIControlStateNormal];
    //添加响应事件
//   [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
//     [self.view addSubview:button];
 //设置响应事件
  //- (void)action:(UIButton *)sender{
  //    NSLog(@"button的点击事件");
  //}

 

*/
    
    
#pragma mark    简单代理
  //RootView.m中
/*
#import "RootView.h"
#import "LTView.h"
    @implementation RootView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setupView];
        }
        return self;
    }
    
    -(void)setupView{
        self.backgroundColor = [UIColor orangeColor];
        LTView *name = [[LTView alloc]initWithFrame:CGRectMake(20, 30, CGRectGetWidth(self.frame) - 40, 40)];
        name.label.text = @"用户名";
        [self addSubview:name];
        
        LTView *pwd = [[LTView alloc]initWithFrame:CGRectMake(20, 90, CGRectGetWidth(name.frame), 40)];
        pwd.label.text = @"密码";
        [self addSubview:pwd];
        
        LTView *mail = [[LTView alloc]initWithFrame:CGRectMake(20, 150, CGRectGetWidth(name.frame), 40)];
        mail.label.text = @"邮箱";
        [self addSubview:mail];
        
    }
*/
   //LTView.h
/*
 #import <UIKit/UIKit.h>
 
 @interface LTView : UIView
 @property (nonatomic,strong)UILabel *label;
 @property (nonatomic,strong)UITextField *field;
 @end
 */
   //LTView.m
/*
 #import "LTView.h"
 
 @implementation LTView
 
 - (instancetype)initWithFrame:(CGRect)frame
 {
 self = [super initWithFrame:frame];
 if (self) {
 [self setupView];
 }
 return self;
 }
 
 - (void)setupView{
 self.label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 60, CGRectGetHeight(self.frame) - 10)];
 self.label.textAlignment = NSTextAlignmentCenter;
 [self addSubview:self.label];
 
 CGFloat x = CGRectGetMaxX(self.label.frame) + 20;
 CGFloat y = CGRectGetMinY(self.label.frame);
 // CGFloat w = CGRectGetWidth(self.label.frame) - 10 - 20;
 CGFloat h = CGRectGetHeight(self.label.frame);
 self.field = [[UITextField alloc]initWithFrame:CGRectMake(x, y, 150, h)];
 self.field.borderStyle = UITextBorderStyleRoundedRect;
 [self addSubview:self.field];
 }
 */

}


#pragma mark        回收键盘
//点击空白回收键盘,(view上的所有控件的键盘)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值