ios开发笔记(一)

1.为应用添加导航栏 navigationController

在appDelegate.m中添加如下代码:

RootViewController *rootVC = [[RootViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.window.rootViewController = navCon;


若不添加导航控制器可这样进行

ViewController *rootViewController = [[ViewController alloc]init];
self.window.rootViewController = rootViewController;



2.UILabel相关属性

    //初始化UILabel 设置坐标位置和高度
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(10,65, 300, 100)];
    //设置label显示的文字
    label1.text = @"标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标签1标";
    //设置文字显示颜色
    label1.textColor = [UIColor blueColor];
    //设置背景yanse
    label1.backgroundColor = [UIColor redColor];
    //设置文字对齐方式
    label1.textAlignment = NSTextAlignmentCenter;
    //设置字体样式(加粗字体)
    //label1.font = [UIFont boldSystemFontOfSize:20];
    //(斜体)注意:若重复设置了label.font会将之前的设置覆盖,以最后一次设置为准
    //label1.font = [UIFont italicSystemFontOfSize:20];
    //设置字体名称为Party LET的字体,大小为30字号
    //label1.font = [UIFont fontWithName:@"Party LET" size:30];
    //设置阴影颜色
    //label1.shadowColor = [UIColor orangeColor];
    //设置阴影偏移量(向右偏移5,相上偏移3)
    //label1.shadowOffset = CGSizeMake(5, 3);
    //设置高亮
    label1.highlighted = YES;
    //设置高亮颜色(若label高亮为NO则高亮颜色不会显示)
    label1.highlightedTextColor = [UIColor blackColor];
    //设置基线位置(只有一行文本这个方法才有效)
    /**
     UIBaselineAdjustmentAlignBaselines -- 文字最上端和label中线对齐(默认)
     UIBaselineAdjustmentAlignCenters   -- 文字中线和label中线对齐
     UIBaselineAdjustmentNone           -- 文字底端和label中线对齐
     */
    label1.baselineAdjustment  = UIBaselineAdjustmentNone;
    
    //当label中文字内容很多显示在屏幕上超过一行才能显示的时候,若不设置人很属性将会把不能显示的文字隐藏起来
    //这时候需要设置最大显示行数,若值为0则不限制显示行数
    label1.numberOfLines = 0;
    //若设置了不限制显示行数 还是显示不下所有文本
    //设置内容超出宽度时的显示模式
    /**
     NSLineBreakByCharWrapping     -- 以字符为单位换行,并且以字符为单位截断
     NSLineBreakByWordWrapping     -- 以单词为单位换行,并且以单词为单位截断
     NSLineBreakByClipping         -- 以单词为单位换行,但是以字符为单位截断
     NSLineBreakByTruncatingHead   -- 以单词为单位换行,省略开头的内容(如果是单行,...在开头显示
                                        如果是多行,则在最后一行的开头显示...)
     NSLineBreakByTruncatingTail   -- 以单词为单位换行,在最后显示...
     NSLineBreakByTruncatingMiddle -- 以单词为单位换行,省略中间的内容
     */
    label1.lineBreakMode = NSLineBreakByWordWrapping;
    //自适应label大小的设置方法
    //1.计算内容大小
    CGSize size = [label1.text sizeWithFont:label1.font constrainedToSize:CGSizeMake(300, 100000) lineBreakMode:NSLineBreakByCharWrapping];
        //2.设置最大显示行数
    label1.numberOfLines = 0;
    //3.用计算出来的size设置label
    label1.frame = CGRectMake(10, 65, size.width, size.height);
    //添加到ViewController中
    [self.view addSubview:label1];


3.UIButton

    //按钮的实例化不需要alloc 需要用到工厂方法创建一个实例
    UIButton *bt1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    bt1.frame = CGRectMake(10, 80, 300, 30);
    //设置按钮背景颜色
    //bt1.backgroundColor = [UIColor blueColor];
    //设置按钮上的文字
    [bt1 setTitle:@"按钮1" forState:UIControlStateNormal];
    [bt1 setTitle:@"按钮被点击" forState:UIControlStateHighlighted];
    //设置按钮文字颜色
    [bt1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //[bt1 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];.
    //设置按钮点击事件
    /*
     Target 执行某一对象中的方法
     action 要执行的方法名
     forControlEvents 触发事件的形式
     按钮点击事件可以触发任何一个类中的任何一个方法,非常灵活
     */
    [bt1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    //设置标签属性,用来区分控件
    bt1.tag = 1;
    //设置按钮高亮状态下是否发光
    bt1.showsTouchWhenHighlighted = YES;
    [self.view addSubview:bt1];
    
    /*
     UIButtonTypeRoundedRect -- 普通圆角按钮
     UIButtonTypeContactAdd  -- 加号按钮
     UIButtonTypeCustom      -- 自定义按钮
     */
    UIButton *bt2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
    bt2.frame = CGRectMake(10, 120, 300, 30);
    [bt2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    //设置tag属性
    bt2.tag = 2;
    [self.view addSubview:bt2];
    //设置自定义按钮
    UIButton *bt3 = [UIButton buttonWithType:UIButtonTypeCustom];
    bt3.frame = CGRectMake(10, 180, 300, 30);
    bt3.tag = 3;
    [bt3 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    //设置高亮状态下图片变暗(一定要有图片才可以,没有图片无效)
    bt3.adjustsImageWhenHighlighted = YES;
    [bt3 setBackgroundImage:[UIImage imageNamed:@"userhead.png"] forState:UIControlStateNormal];
    //设置按钮上的图片
    //[bt3 setImage:[UIImage imageNamed:@"userhead.png"] forState:UIControlStateNormal];
    [bt3 setTitle:@"按钮1" forState:UIControlStateNormal];
    
    //[bt3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.view addSubview:bt3];
    
    //按钮点击事件处理的方法
    -(void)btnClick:(id)sender
    {
       UIView *view = (UIView *)sender;
       if(view.tag == 1){
           NSLog(@"按钮被点击");
       }else if(view.tag == 2){
           NSLog(@"jiahao 按钮被点击");
       }else if(view.tag == 3){
           NSLog(@"自定义按钮被点击");
       }
    }

 


  4.UIImageView


    UIImage *image = [UIImage imageNamed:@"userhead.png"];
    UIImageView *iv1 = [[UIImageView alloc]initWithImage:image];
    //iv1.frame =CGRectMake(10, 65, image.size.width,image.size.height);
    iv1.frame =CGRectMake(10, 65, 300,300);
    iv1.backgroundColor = [UIColor greenColor];
    //设置内容填充模式
    /*
     UIViewContentModeCenter            -- 填充到中间
     UIViewContentModeScaleToFill       -- 变形以充满整个View(比例可能会改变)
     UIViewContentModeScaleAspectFill   -- 图片被拉伸(比例不变),小的一边充满整个View,大的一边可能会超过
     UIViewContentModeScaleAspectFit    -- 图片被拉伸(比例不变),大的一边充满整个View
     */
    iv1.contentMode = UIViewContentModeCenter;
    iv1.hidden = YES;
    [self.view addSubview:iv1];
    
    NSMutableArray *images = [[NSMutableArray alloc]init];
    for (int i = 1; i<=13; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"png%d.png",i]];
        [images addObject:image];
    }
    UIImageView *iv2 = [[UIImageView alloc]init];
    iv2.frame = CGRectMake(10, 150, 300, 100);
    //设置动画数组
    iv2.animationImages = images;
    //设置动画周期时间
    iv2.animationDuration = 1;
    //设置动画动画播放次数(如果不设置次数则一直播放)
    iv2.animationRepeatCount = 5;
    [iv2 startAnimating];
    if (iv2.isAnimating) {
        [iv2 stopAnimating];
    }

5.UIProgressView

    UIProgressView *progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    progressView.frame = CGRectMake(10, 100, 300, 30);
    //progressView.backgroundColor = [UIColor redColor];
    //设置进度值
    progressView.progress = 0.5;
    //设置进度条高亮颜色
    progressView.progressTintColor = [UIColor redColor];
    //设置轨道高亮颜色
    progressView.trackTintColor = [UIColor blackColor];
    //设置进度条图片
    //progressView.progressImage = [UIImage imageNamed:@"userhead.png"];
    //设置轨道进度条图片
    //progressView.trackImage = [UIImage imageNamed:@"userhead.png"];
    //设置进度并带有动画(轨道和进度条都带有动画)
    [progressView setProgress:0.8 animated:YES];
    [self.view addSubview:progressView];


6.UISlider

    UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(10, 100, 300, 100)];
    slider.backgroundColor = [UIColor redColor];
    //设置当前值
    slider.value = 0.5;
    //设置滑动事件
    [slider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
    //设置最小值(若设置的最小值小于1且未设置最大值,那么滑动条是不能滑动的)
    slider.minimumValue = 100;
    //设置最大值
    slider.maximumValue = 200;
    //设置最小值一侧的图片
    //slider.minimumValueImage = [UIImage imageNamed:@"userhead.png"];
    //设置最大值一侧的图片
    //slider.maximumValueImage = [UIImage imageNamed:@"userhead.png"];
    //设置最小值轨道颜色
    slider.minimumTrackTintColor = [UIColor blackColor];
    //设置最大值轨道颜色
    slider.maximumTrackTintColor = [UIColor blueColor];
    //设置滑块颜色(ios7无效)
    slider.thumbTintColor = [UIColor orangeColor];
    //设置滑块图片
    //[slider setThumbImage:(UIImage *) forState:(UIControlState)];
    [self.view addSubview:slider];


7.UITextField

    UITextField *textFeild = [[UITextField alloc]initWithFrame:CGRectMake(10, 80, 300, 100)];
    //设置
    textFeild.borderStyle = UITextBorderStyleRoundedRect;
    textFeild.backgroundColor = [UIColor redColor];
    //设置填充文本
    textFeild.placeholder = @"请输入密码";
    //设置是否明文输入
    textFeild.secureTextEntry =NO;
    //设置键盘弹出输入方式
    textFeild.keyboardType = UIKeyboardTypeDefault;
    //设置键盘弹出样式
    textFeild.keyboardAppearance = UIKeyboardAppearanceDefault ;
    //设置是否再次输入时清空文本
    textFeild.clearsOnBeginEditing = YES;
    //设置纵向对齐方式
    textFeild.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    //设置横向对齐方式
    textFeild.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //设置滚动方式
    textFeild.adjustsFontSizeToFitWidth = YES;
    //设置最小字号(和滚动相关)
    textFeild.minimumFontSize = 50;
    //设置字符大写样式
    textFeild.autocapitalizationType = UITextAutocapitalizationTypeWords;
    //设置return建样式
    textFeild.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:textFeild];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值