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

来源:互联网 发布:淘宝客文案怎么写 编辑:程序博客网 时间:2024/04/29 06:39
#pragma mark -UILabel
- (void)_initLabel
{
   
UILabel *textLable = [[UILabelalloc]initWithFrame:CGRectMake(10,30,150,250)];
    textLable.
backgroundColor= [UIColorgrayColor];
   
//设置文本内容
    textLable.
text= @"good morning hehehehehe good morning hehehehehe";
   
//设置字体, systemFont使用系统的字体,大小10
    textLable.
font= [UIFontsystemFontOfSize: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= [UIColororangeColor];
    //设置文本对齐方式
    textLable.textAlignment= NSTextAlignmentCenter;
    //设置当前的显示行数,默认是1,如果设为0,是自动换行
    textLable.numberOfLines= 0;
    //自动根据文本调整宽度和高度
    [textLable
sizeToFit];
//    NSLog(@"textLabel is %@", textLable);
    [self.windowaddSubview:textLable];
}

#pragma mark -UIButton
- (void)_initButton
{
   
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
    button.
frame= CGRectMake(10,180,90,44);
    button.
backgroundColor= [UIColorgreenColor];
   
//设置显示标题,标题总是需要跟状态绑定到一起的
//    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= [UIFontboldSystemFontOfSize:20];
    //设置标题的颜色
    [button
setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
    //获取图片  imageNamed:在当前工程目录下找到名字为“”的图片,如果图片是png格式的,可以不加.png,但是建议还是添加上
   
UIImage *image1 = [UIImageimageNamed:@"back_on_black.png"];
   
UIImage *image2 = [UIImageimageNamed:@"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:selfaction:@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.windowaddSubview:button];
}
- (void)btnClick:(UIButton*)btn
{
   
NSLog(@"btnClick");
//    btn.selected = !btn.selected;
}

#pragma mark -UITextField
//文本输入框
- (void)_initTextField
{
    //如果需要在模拟器中调用电脑的键盘使用快捷键:command+ shift+ k
    UITextField *textField = [[UITextFieldalloc]initWithFrame:CGRectMake(100,100,200,30)];
    textField.tag= 100;
   
//设置输入框的边框样式,默认无边框
    textField.
borderStyle= UITextBorderStyleRoundedRect;
   
//设置输入文字的字体
    textField.
font= [UIFontboldSystemFontOfSize:18];
   
//设置文字的颜色
    textField.
textColor= [UIColorredColor];
   
//设置或者获取当前文本输入框的内容
//    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.windowaddSubview:textField];
    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeContactAdd];
    [btn
addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
    btn.
frame= CGRectMake(0,100,50,50);
    [self.windowaddSubview:btn];
}
- (void)btnClick:(UIButton*)btn
{
    UITextField *textField = (UITextField*)[self.windowviewWithTag: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 = [UIImageimageNamed:@"scene2.jpg"];
    UIImageView *imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,100,300,100)];
//    imgView.image = img;
    imgView.backgroundColor= [UIColorredColor];
    //设置ImgView高亮状态下的图片
//    imgView.highlightedImage = [UIImage imageNamed:@"scene1.jpg"];
//    imgView.highlighted = YES;  //设置为高亮状态
   
   
//设置内容模式
   
/*
     UIViewContentModeScaleToFill, //
横向和纵向都拉伸到边框
     UIViewContentModeScaleAspectFit, //
等比例拉伸,当某一方向到头时,则不拉伸
     UIViewContentModeScaleAspectFill, //
等比例拉伸,当某一方向到头时,则继续拉伸到另一方向也到头
     */
    imgView.contentMode= UIViewContentModeScaleAspectFit;
    //imgView设置动画图片组
   
UIImage *img1 = [UIImageimageNamed:@"scene1.jpg"];
   
UIImage *img2 = [UIImageimageNamed:@"scene2.jpg"];
   
UIImage *img3 = [UIImageimageNamed:@"scene3.jpg"];
   
UIImage *img4 = [UIImageimageNamed:@"scene4.jpg"];
    UIImage *img5 = [UIImageimageNamed:@"scene5.jpg"];
    NSArray *images = @[img1, img2, img3, img4, img5];
    //设置动画播放的集合
    imgView.animationImages= images;
    //设置播放动画的时间
    imgView.
animationDuration= 5;
   
//开始动画
//    [imgView startAnimating];
    [self.windowaddSubview:imgView];
    UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeContactAdd];
    btn.
frame= CGRectMake(0,50,40,40);
    [btn
addTarget:selfaction:@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 = [[UISlideralloc]initWithFrame:CGRectMake(10,100,300,30)];
//    slider.backgroundColor = [UIColor redColor];
    [self.windowaddSubview:slider];
    //设置滑动的最大值和最小值
    slider.
maximumValue= 100;
    slider.minimumValue= 0;
    //设置初始值
    slider.value= 50;
    //设置滑动条左边|右边的颜色
//    [slider setMinimumTrackTintColor:[UIColor greenColor]];
//    [slider setMaximumTrackTintColor:[UIColor yellowColor]];
    //设置滑动条的左右边的图片
   
UIImage *image1 = [UIImageimageNamed:@"com_slider_min_l-Decoded"];
    UIImage *image2 = [UIImageimageNamed:@"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:image1forState:UIControlStateNormal];
    [slider setMaximumTrackImage:image2 forState:UIControlStateNormal];
    //设置滑块的图片
   
UIImage *image3 = [UIImageimageNamed:@"com_thumb_max_n-Decoded"];
    UIImage *image4 = [UIImageimageNamed:@"com_thumb_max_h-Decoded"];
    [slider setThumbImage:image3forState:UIControlStateNormal];
    [slider setThumbImage:image4 forState:UIControlStateHighlighted];
    //设置滑块按钮的颜色
//    [slider setThumbTintColor:[UIColor redColor]];
    //添加事件
    [slider addTarget:selfaction:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged]; 
}
- (void)sliderAction:(UISlider*)slider
{
   
NSLog(@"slider value is %.2f", slider.value);
}

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

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

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

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


//进度条--------------UIProgressView
- (void)_initProgress
{
   
UIProgressView *progress = [[UIProgressViewalloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    progress.
frame= CGRectMake(10,300,300,0);
    progress.
backgroundColor= [UIColorredColor]; //无效
   
//设置进度值
//    progress.progress = 0.5;
   
//设置已经加载的进度条的颜色
    progress.
progressTintColor= [UIColorgreenColor];
   
//设置未加载的进度条的颜色
    progress.
trackTintColor= [UIColoryellowColor];
    [
self.windowaddSubview:progress];
   
    [
NSTimerscheduledTimerWithTimeInterval: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];
    }
}


0 0
原创粉丝点击