UI连载五--常用UI控件—Label、Button、UITextField、UIImageView、UISlider、UISwitch、UIActivityIndicatorView等

#pragma mark -UILabel
- ( void )_initLabel
{
   
UILabel *textLable = [[ UILabel alloc ] initWithFrame : CGRectMake ( 10 , 30 , 150 , 250 )];
    textLable.
backgroundColor = [ UIColor grayColor ];
   
// 设置文本内容
    textLable.
text = @"good morning hehehehehe good morning hehehehehe" ;
   
// 设置字体 , systemFont 使用系统的字体,大小 10
    textLable.
font = [ UIFont systemFontOfSize : 16 ];
   
// 设置粗体
//    textLable.font = [UIFont boldSystemFontOfSize:16];
    // 字体类 UIFont
//    NSArray *familyNames = [UIFont familyNames];
//    NSLog(@"familyNames is %@", familyNames);
//    textLable.font = [UIFont fontWithName:@"Zapf Dingbats" size:16];
   
   
// 设置字体颜色
    textLable.textColor = [UIColor orangeColor];
    // 设置文本对齐方式
    textLable.textAlignment = NSTextAlignmentCenter;
    // 设置当前的显示行数,默认是 1 , 如果设为 0 ,是自动换行
    textLable.numberOfLines = 0;
    // 自动根据文本调整宽度和高度
    [textLable
sizeToFit ];
//    NSLog(@"textLabel is %@", textLable);
    [ self . window addSubview :textLable];
}

#pragma mark -UIButton
- ( void )_initButton
{
   
UIButton *button = [ UIButton buttonWithType : UIButtonTypeCustom ];
    button.
frame = CGRectMake ( 10 , 180 , 90 , 44 );
    button.
backgroundColor = [ UIColor greenColor ];
   
// 设置显示标题, 标题总是需要跟状态绑定到一起的
//    button.titleLabel.text = @"hehe";   // 错误,不能这样设置 title
   
/*
     typedef NS_OPTIONS(NSUInteger, UIControlState) {
     UIControlStateNormal       = 0,
     UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
     UIControlStateDisabled     = 1 << 1,
     UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
     UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
     UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
     };
     */

    [button
setTitle : @"hehe" forState : UIControlStateNormal ];
   
// 设置高亮状态下的 title
//    [button setTitle:@"haha" forState:UIControlStateHighlighted];
   
// 设置选中状态下的 title
//    [button setTitle:@"hihi" forState:UIControlStateSelected];
    // 设置按钮是否选中
//    button.selected = true;
    // 设置标题的字体
    button.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    // 设置标题的颜色
    [button
setTitleColor :[ UIColor redColor ] forState : UIControlStateNormal ];
//    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
    // 获取图片   imageNamed: 在当前工程目录下找到名字为 “” 的图片 , 如果图片是 png 格式的,可以不加 .png ,但是建议还是添加上
   
UIImage *image1 = [ UIImage imageNamed : @"back_on_black.png" ];
   
UIImage *image2 = [ UIImage imageNamed : @"back_on.png" ];

   
// 设置背景图片 ----- 图片会随着按钮的 frame 改变而拉伸
//    [button setBackgroundImage:image1 forState:UIControlStateNormal];
//    // 设置高亮状态下的图片
//    [button setBackgroundImage:image2 forState:UIControlStateHighlighted];
   
   
// 设置图片 ----- 图片不会被拉伸 -- 但是 imageView 上面不能显示 Title
    [button
setImage :image1 forState : UIControlStateNormal ];
    [button setImage:image2 forState:UIControlStateHighlighted];
    // 给按钮添加点击事件
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    // 设置 titleLabel 的偏移量
//    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -100, 0, 0)];
//    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
    // 设置按钮是否可用
//    button.enabled = NO;
    // 是否相应触摸时间
//    button.userInteractionEnabled = NO;
    [ self . window addSubview :button];
}
- ( void )btnClick:( UIButton *)btn
{
   
NSLog ( @"btnClick" );
//    btn.selected = !btn.selected;
}

#pragma mark -UITextField
// 文本输入框
- (void)_initTextField
{
    // 如果需要在模拟器中调用电脑的键盘 使用快捷键: command+ shift+ k
    UITextField *textField = [[ UITextField alloc ] initWithFrame : CGRectMake ( 100 , 100 , 200 , 30 )];
    textField. tag = 100 ;
   
// 设置输入框的边框样式,默认无边框
    textField.
borderStyle = UITextBorderStyleRoundedRect ;
   
// 设置输入文字的字体
    textField.
font = [ UIFont boldSystemFontOfSize : 18 ];
   
// 设置文字的颜色
    textField.
textColor = [ UIColor redColor ];
   
// 设置或者获取当前文本输入框的内容
//    textField.text = @"hehehehe";
   
// 设置对齐方式
    textField.
textAlignment = NSTextAlignmentCenter ;
   
// 设置首字母是否自动大写
//    textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
   
// 设置自动单词提示
//    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    // 输入框为空时的提示文本
    textField.placeholder = @" 请输入登陆邮箱 " ;
    // 修改键盘上的 return 按钮上的标题
    textField.returnKeyType = UIReturnKeyGo;
    // 设置输入框是否安全输入
//    textField.secureTextEntry = YES;
    // 纯数字键盘
//    textField.keyboardType = UIKeyboardTypeNumberPad;
    // 开启清楚按钮
    textField. clearButtonMode = UITextFieldViewModeWhileEditing ;
//    // 成为第一响应者, 弹出键盘
//    [textField becomeFirstResponder];
//    // 失去第一响应者, 收起键盘
//    [textField resignFirstResponder];
   
    // 设置代理
    textField.delegate = self;
    [ self . window addSubview :textField];
    UIButton *btn = [ UIButton buttonWithType : UIButtonTypeContactAdd ];
    [btn
addTarget : self action : @selector (btnClick:) forControlEvents : UIControlEventTouchUpInside ];
    btn.
frame = CGRectMake ( 0 , 100 , 50 , 50 );
    [self.window addSubview:btn];
}
- ( void )btnClick:( UIButton *)btn
{
    UITextField *textField = (UITextField *)[self.window viewWithTag:100];
    [textField resignFirstResponder ];
}

#pragma mark -UITextFieldDelegate
// 将要开始编辑
- (
BOOL )textFieldShouldBeginEditing:( UITextField *)textField
{
   
NSLog ( @" 将要开始编辑 " );
   
// 返回 YES 表示可以继续编辑,返回 NO 是阻止编辑
   
return YES ;
}
// 已经开始编辑了
- (
void )textFieldDidBeginEditing:( UITextField *)textField
{
   
NSLog ( @" 正在编辑 " );
}
// 已经结束编辑
- ( void )textFieldDidEndEditing:( UITextField *)textField
{
   
NSLog ( @" 已经结束 " );
}
// 输入框的内容被修改的时候调用的协议方法 ---- 重要
- (
BOOL )textField:( UITextField *)textField shouldChangeCharactersInRange:( NSRange )range replacementString:( NSString *)string
{
   
NSString *lastStr = textField. text ;
   
NSString *rangeStr = NSStringFromRange (range);
    NSString *replaceStr = string;
    NSLog ( @"lastStr is %@" , lastStr);
    NSLog ( @"rangeStr is %@" , rangeStr);
    NSLog ( @"replaceStr is %@" , replaceStr);
    return YES ;
}

#pragma mark -UIImageView
// 图片视图( ImageView
- (
void )_initImageView
{
   
// 创建图片对象
    UIImage *img = [UIImage imageNamed:@"scene2.jpg"];
    UIImageView *imgView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 100 , 300 , 100 )];
//    imgView.image = img;
    imgView.backgroundColor = [UIColor redColor];
    // 设置 ImgView 高亮状态下的图片
//    imgView.highlightedImage = [UIImage imageNamed:@"scene1.jpg"];
//    imgView.highlighted = YES;  // 设置为高亮状态
   
   
// 设置内容模式
   
/*
     UIViewContentModeScaleToFill, //
横向和纵向都拉伸到边框
     UIViewContentModeScaleAspectFit, //
等比例拉伸,当某一方向到头时,则不拉伸
     UIViewContentModeScaleAspectFill, //
等比例拉伸,当某一方向到头时,则继续拉伸到另一方向也到头
     */
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    //imgView 设置动画图片组
   
UIImage *img1 = [ UIImage imageNamed : @"scene1.jpg" ];
   
UIImage *img2 = [ UIImage imageNamed : @"scene2.jpg" ];
   
UIImage *img3 = [ UIImage imageNamed : @"scene3.jpg" ];
   
UIImage *img4 = [ UIImage imageNamed : @"scene4.jpg" ];
    UIImage *img5 = [UIImage imageNamed:@"scene5.jpg"];
    NSArray *images = @[img1, img2, img3, img4, img5];
    // 设置动画播放的集合
    imgView.animationImages = images;
    // 设置播放动画的时间
    imgView.
animationDuration = 5 ;
   
// 开始动画
//    [imgView startAnimating];
    [self.window addSubview:imgView];
    UIButton *btn = [ UIButton buttonWithType : UIButtonTypeContactAdd ];
    btn.
frame = CGRectMake ( 0 , 50 , 40 , 40 );
    [btn
addTarget : self action : @selector (btnClick:) forControlEvents : UIControlEventTouchUpInside ];
   
// 注意: imageView 默认的触摸事件是关闭的
    imgView.userInteractionEnabled = YES;
    [imgView addSubview:btn];
}
- ( void )btnClick:( UIButton *)btn
{
   
NSLog ( @"hehehe" );
}

#pragma mark -UISlider
// 滑块
- ( void )_initSlider
{
    //slider 显示的高度是固定的
    UISlider *slider = [[ UISlider alloc ] initWithFrame : CGRectMake ( 10 , 100 , 300 , 30 )];
//    slider.backgroundColor = [UIColor redColor];
    [ self . window addSubview :slider];
    // 设置滑动的最大值和最小值
    slider.
maximumValue = 100 ;
    slider.minimumValue = 0;
    // 设置初始值
    slider.value = 50;
    // 设置滑动条左边 | 右边的颜色
//    [slider setMinimumTrackTintColor:[UIColor greenColor]];
//    [slider setMaximumTrackTintColor:[UIColor yellowColor]];
    // 设置滑动条的左右边的图片
   
UIImage *image1 = [ UIImage imageNamed : @"com_slider_min_l-Decoded" ];
    UIImage *image2 = [UIImage imageNamed:@"com_slider_min_r-Decoded"];
    // 设置图片的拉伸点
//    image1 = [image1 stretchableImageWithLeftCapWidth:5 topCapHeight:0];
//    image2 = [image2 stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    image1 = [image1 resizableImageWithCapInsets : UIEdgeInsetsMake ( 0 , 5 , 0 , 5 )];
    image2 = [image2 resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
    [slider setMinimumTrackImage :image1 forState : UIControlStateNormal ];
    [slider setMaximumTrackImage:image2 forState:UIControlStateNormal];
    // 设置滑块的图片
   
UIImage *image3 = [ UIImage imageNamed : @"com_thumb_max_n-Decoded" ];
    UIImage *image4 = [UIImage imageNamed:@"com_thumb_max_h-Decoded"];
    [slider setThumbImage :image3 forState : UIControlStateNormal ];
    [slider setThumbImage:image4 forState:UIControlStateHighlighted];
    // 设置滑块按钮的颜色
//    [slider setThumbTintColor:[UIColor redColor]];
    // 添加事件
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];  
}
- ( void )sliderAction:( UISlider *)slider
{
   
NSLog ( @"slider value is %.2f" , slider. value );
}

#pragma mark -UISwitch
- ( void )_initSwitch
{
   
UISwitch *switchUI = [[ UISwitch alloc ] initWithFrame : CGRectMake ( 10 , 200 , 100 , 100 )];
    [self.window addSubview:switchUI];
    // 默认开
    switchUI.on = YES;
    // 添加事件
    [switchUI addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];   
}
- ( void )switchAction:( UISwitch *)switchUI
{
   
if (switchUI. on ) {
       
NSLog ( @" 开启 " );
    }
else {
       
NSLog ( @" 关闭 " );
    }
}

//风火轮-------------UIActivityIndicatorView
- ( void )_UIActivityIndicatorView
{
   
UIActivityIndicatorView *activity = [[ UIActivityIndicatorView alloc ] initWithActivityIndicatorStyle : UIActivityIndicatorViewStyleGray ];
    activity.
frame = CGRectMake ( 100 , 100 , 0 , 0 );
    activity.
backgroundColor = [ UIColor redColor ];
   
// 开始转动
    [activity
startAnimating ];
   
   
// 停止转动并且隐藏
//    [activity stopAnimating];
   
    [
self . window addSubview :activity];
}

//页面控件--------------UIPageControl
- ( void )_UIPageControl
{
   
UIPageControl *pageControl = [[ UIPageControl alloc ] initWithFrame : CGRectMake ( 100 , 200 , 200 , 20 )];
   
// 设置总页数
    pageControl.
numberOfPages = 5 ;
   
// 设置当前选中的页面的索引
    pageControl.
currentPage = 2 ;
   
// 添加点击事件
    [pageControl
addTarget : self action : @selector (pageAction:) forControlEvents : UIControlEventValueChanged ];
    pageControl.
backgroundColor = [ UIColor redColor ];
    [
self . window addSubview :pageControl];
}

- (
void )pageAction:( UIPageControl *)pageControl
{
   
NSLog ( @"pageControl.currentPage is %ld" , pageControl. currentPage );
}


//进度条--------------UIProgressView
- ( void )_initProgress
{
   
UIProgressView *progress = [[ UIProgressView alloc ] initWithProgressViewStyle : UIProgressViewStyleDefault ];
    progress.
frame = CGRectMake ( 10 , 300 , 300 , 0 );
    progress.
backgroundColor = [ UIColor redColor ];  // 无效
   
// 设置进度值
//    progress.progress = 0.5;
   
// 设置已经加载的进度条的颜色
    progress.
progressTintColor = [ UIColor greenColor ];
   
// 设置未加载的进度条的颜色
    progress.
trackTintColor = [ UIColor yellowColor ];
    [
self . window addSubview :progress];
   
    [
NSTimer scheduledTimerWithTimeInterval : 0.2
                                    
target : self
                                  
selector : @selector (timeAction:)
                                  
userInfo :progress
                                   
repeats : true ];
   
}

- (
void )timeAction:( NSTimer *)timer
{
   
UIProgressView *progress = timer. userInfo ;
    progress.
progress += 0.01 ;
   
   
if (progress. progress >= 1 ) {
        [timer
invalidate ];
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值