iOS开发关于基本控件的初始化及使用方法

作者: 西西哈哈 | 来源:发表于2016-04-09 20:58 被阅读2647次

    在众多移动应⽤用中,能看到各式各样的表格数据 。
    在iOS中,要实现表格,数据,图片添加,按钮点击等方法的展示,最常用的做法就是使用UI中的基本控件来完成 。

    首先在AppDelegate.m里初始化window:

        #import "AppDelegate.h"
    
        @interface AppDelegate ()
    
        @end
    
        @implementation AppDelegate
        - (void)dealloc
        {
        [_window release];                  //释放window
        [super dealloc];
        }
    #pragma make -- app启动完成走这个方法
      - (BOOL)application:(UIApplication *)application       didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    #//初始化Window对象
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    #//设置颜色:
    self.window.backgroundColor = [UIColor whiteColor];
    #//让window可见:
    [self.window makeKeyAndVisible];
    #//为window创建根视图控制器:
    UIViewController *vc = [[UIViewController alloc] init];
    #//将vc设置为window的根视图控制器:
    self.window.rootViewController = vc;
    

    UILable : (标签控件的常用属性)

      //初始化标签控件:
         UILabel *label = [[UILabel alloc]  
         initWithFrame:CGRectMake(100, 200, 175, 100)];
    
    //设置背景颜色:
    label.backgroundColor = [UIColor grayColor];
    //设置label要显示的文字:
    label.text = @"Hello student!";
    //设置文字对齐方式:(左中右三种 0.1.2)
    label.textAlignment = NSTextAlignmentCenter;    //给1也行
    //设置字体颜色:
    label.textColor = [UIColor yellowColor];
    //设置字体和大小:
    //    label.font = [UIFont fontWithName:@"Zapfino" size:20];
      //设置显示的行数(换行):(0表示自动去换行)
    label.numberOfLines = 0;
    //设置断行模式:
     label.lineBreakMode = NSLineBreakByWordWrapping;   //按 
     字母用Char换Word
    

    UITextField :(文本框的常用属性)

     //初始化文本框:
     UITextField *textfield = [[UITextField alloc] 
    initWithFrame:CGRectMake(100, 100, 175, 50)];
    
    //设置提示文字:
    textfield.placeholder = @"请输入密码:";
    
    //设置边框样式:
    textfield.borderStyle = UITextBorderStyleRoundedRect;
    
    //设置边框宽度和颜色:
    textfield.layer.borderWidth = 5.0;
    textfield.layer.borderColor = [UIColor blueColor].CGColor
    
    //设置文本颜色:
    textfield.textColor = [UIColor redColor];
    
    //设置文本对齐方式:(0.1.2  左 中 右)
    textfield.textAlignment = 0;
    
    //设置字体:
    textfield.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];    
     #注意字体的书写一定要标准
    
    //是否允许编辑:
    textfield.enabled = YES;  //(NO 不可编辑,默认为yes)
    
    //开始输入时清空之前的内容:
    textfield.clearsOnBeginEditing = NO;    //(NO就清空了,默认为 YES)
    
    //设置密码输入模式(小点):
    textfield.secureTextEntry = YES;      //no为正常输入
    
    //设置键盘的样式:
     //    textfield.keyboardType = UIKeyboardTypeDefault;     
    //  比如只能输入数字textfield.keyboardType =   UIKeyboardTypeNumberPad;
    
    //设置键盘右下角按钮内容:
    textfield.returnKeyType = UIReturnKeyNext;  //有很多种
    

    UIButton :(按钮的常用属性)

     //初始化按钮:
     UIButton *button = [UIButton   buttonWithType:UIButtonTypeCustom];
    //设置位子
    button.frame = CGRectMake(100, 100, 175, 64);
    //设置按钮文字:
    [button setTitle:@"我是按钮" forState:UIControlStateNormal];
    
     //设置边框:
     //    button.layer.borderWidth = 2.0;
    
     //设置边框颜色:
    //    button.layer.borderColor= [UIColor greenColor].CGColor;
    
    //设置背景颜色
     // button.backgroundColor = [UIColor blackColor];
    
    //设置圆角:
    //    button.layer.cornerRadius = 10;
     
      //设置背景图:
     //    [button setBackgroundImage:[UIImage    
     imageNamed:@"1.jpg"] forState:UIControlStateNormal];
    
    
      //设置前景图
     //    [button setImage:[UIImage imageNamed:@"666.jpg"] 
     forState:UIControlStateNormal];
    //    UIImage *frontImage = [button  
     imageForState:UIControlStateNormal];
    //    NSLog(@"%@", frontImage);
    
    
      //    //设置按钮字体颜色:
      [button setTitleColor:[UIColor redColor] 
     forState:UIControlStateNormal];
    
    //修改字体大小以及字体:
    //修改字体大小以及字体:
       button.titleLabel.font = [UIFont fontWithName:@"Zapfino" 
    size:20];
    //获取字体颜色:
       UIColor *wordColor = [button  
    titleColorForState:UIControlStateNormal];
    NSLog(@"%@", wordColor);
    
    //为按钮button添加点击事件:
    [button addTarget:self action:@selector(didClickedDogButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //为按钮移除点击事件
    [button removeTarget:self action:@selector(didClickedDogButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //最后将按钮添加到window上
     [view addSubview:button];
    
    #注意:按钮是系统自动释放 不用再release ,否则将会过度释放
    

    UIImageView :(图片显示控件的常用属性)

    //创建UIImageView对象:
    UIImageView *imageView = [[UIImageView   
    alloc]initWithFrame:self.window.frame];
    //设置颜色:
    //    imageView.backgroundColor = [UIColor orangeColor];
    //为imageView设置图片:
    //    imageView.image = [UIImage imageNamed:@"1.jpg"];
    
    更多图片的属性和方法以后会介绍,这里简单为大家做个植物大战僵尸中每个图片及方法的实现,希望能帮助大家理解:

    # 首先我们需要将已有的图片文件拖到自己的工程里:比如下面

    BackGround.png
     //1).设置整体背景:   
     imageView.image = [UIImage     
     imageNamed:@"BackGround"];
     #这里以向日癸为例,僵尸,瓜等同理
     (因为没找到符合简书规格的图片,暂不上传 sorry)
     //2).循环创建动画素材(UIImage对象)
    NSMutableArray *flowerArray = [NSMutableArray array];
    
    for (NSInteger i = 1; i <= 18; i++) {
        //拼接每个图片的名字:
    
        NSString *name = [NSString stringWithFormat:@"flower%ld.tiff", i];
        UIImage *image = [UIImage imageNamed:name];
        [flowerArray addObject:image];
    }
    //3).创建单独的UIImageView对象
    UIImageView *flowerIamgeView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 80, 73, 74)];
    [imageView addSubview:flowerIamgeView];
    [flowerIamgeView release];
    //4).将数组赋值给imageView
    flowerIamgeView.animationImages = flowerArray;//********
    //5).设置播放图片的时间间隔(两张图片之间播放的时间差):
    flowerIamgeView.animationDuration = 1.0f;
    //6).设置一下重复次数:
    //    flowerIamgeView.animationRepeatCount = 5;
    #当重复次数为0时就一直保持图片动态播放
    //7).开始动画
    [flowerIamgeView startAnimating];
    
    //花动起来:
    [UIView animateWithDuration:10 animations:^{
         flowerIamgeView.frame = CGRectMake(200, 400, 146, 148);
    }];
    

    相关文章

      网友评论

        本文标题:iOS开发关于基本控件的初始化及使用方法

        本文链接:https://www.haomeiwen.com/subject/iugjlttx.html