UI初级第三课  常用UI控件——iOS学习连载17

1.文本UILabel
    UILabel *textLable = [[ UILabel alloc ] initWithFrame : CGRectMake ( 10 , 30 , 150 , 250 )];
(1)设置文本内容 textLable. text = @"good morning hehehehehe good morning hehehehehe" ;
(2)设置字体 , systemFont 使用系统的字体: textLable. font = [ UIFont systemFontOfSize : 16 ];
(3)设置粗体 textLable.font = [UIFont boldSystemFontOfSize:16];
(4)设置字体颜色 textLable. textColor = [ UIColor orangeColor ];
(5)设置文本对齐方式 textLable. textAlignment = NSTextAlignmentCenter ;
(6)设置当前的显示行数,默认是 1 , 如果设为 0 ,是自动换行: textLable. numberOfLines = 0 ;
(7)自动根据文本调整宽度和高度 [textLablesizeToFit];
2.按钮 UIButton
UIButton *button = [ UIButton buttonWithType : UIButtonTypeCustom ];
button.frame =CGRectMake(10,180, 90,44);
(1)设置显示标题, 标题总是需要跟状态绑定到一起的: [button setTitle : @"hehe" forState : UIControlStateNormal ];
(2)设置高亮状态下的 title: [button setTitle:@"haha" forState:UIControlStateHighlighted];
(3)设置选中状态下的 title: [button setTitle:@"hihi" forState:UIControlStateSelected];
(4)设置按钮是否选中 button.selected = true;
(5)设置标题的字体 button. titleLabel . font = [ UIFont boldSystemFontOfSize : 20 ];
(6)设置标题的颜色 [button setTitleColor :[ UIColor redColor ] forState : UIControlStateNormal ];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
(7)获取图片   imageNamed: 在当前工程目录下找到名字为 “” 的图片 , 如果图片是 png 格式的,可以不加 .png ,但是建议还是添加上:
UIImage *image1 = [ UIImage imageNamed : @"back_on_black.png" ];
UIImage *image2 = [ UIImage imageNamed : @"back_on.png" ];
(8)设置背景图片 ----- 图片会随着按钮的 frame 改变而拉伸 [button setBackgroundImage:image1 forState:UIControlStateNormal];
(9)设置高亮状态下的图片 [button setBackgroundImage:image2 forState:UIControlStateHighlighted];
(10)设置图片 ----- 图片不会被拉伸 -- 但是 imageView 上面不能显示 Title
[button setImage :image1 forState : UIControlStateNormal ];
[button setImage :image2 forState : UIControlStateHighlighted ];
(11)给按钮添加点击事件 [button addTarget : self action : @selector (btnClick:) forControlEvents : UIControlEventTouchUpInside ];
(12)设置 titleLabel 的偏移量:
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -100, 0, 0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0, 30, 0, 0)];
(13)设置按钮是否可用 button.enabled = NO;
(14)是否相应触摸时间 button.userInteractionEnabled = NO;
3.文本输入框UITextField
如果需要在模拟器中调用电脑的键盘 使用快捷键: command+ shift+ k
UITextField *textField = [[ UITextField alloc ] initWithFrame : CGRectMake ( 100 , 100 , 200 , 30 )];
(1)设置标签值:textField.tag =100;
(2) 设置输入框的边框样式,默认无边框 textField. borderStyle = UITextBorderStyleRoundedRect ;
(3) 设置输入文字的字体 textField. font = [ UIFont boldSystemFontOfSize : 18 ];
(4) 设置文字的颜色 textField. textColor = [ UIColor redColor ];
(5)设置或者获取当前文本输入框的内容: textField.text = @"hehehehe";
(6) 设置对齐方式 textField. textAlignment = NSTextAlignmentCenter ;
(7) 设置首字母是否自动大写 textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
(8)设置自动单词提示 textField.autocorrectionType = UITextAutocorrectionTypeNo;
(9)输入框为空时的提示文本 textField. placeholder = @" 请输入登陆邮箱 " ;
(10)修改键盘上的 return 按钮上的标题 textField. returnKeyType = UIReturnKeyGo ;
(11)设置输入框是否安全输入 textField.secureTextEntry = YES;
(12)纯数字键盘 textField.keyboardType = UIKeyboardTypeNumberPad;
(13) 开启清除按钮 textField. clearButtonMode = UITextFieldViewModeWhileEditing ;
(14)成为第一响应者, 弹出键盘 [textField becomeFirstResponder];
(15)失去第一响应者, 收起键盘 [textField resignFirstResponder];
(16) 设置代理 textField.delegate = self;
(17)输入框的内容被修改的时候调用的协议方法----重要
- (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;
}
4.图像视图UIImageView
UIImageView *imgView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 100 , 300 , 100 )];
(1)设置背景颜色 imgView. backgroundColor = [ UIColor redColor ];
(2)设置背景图片: UIImage *img = [ UIImage imageNamed : @"scene2.jpg" ];
                imgView.image = img;
(3)设置 ImgView 高亮状态下的图片 imgView.highlightedImage = [UIImage imageNamed:@"scene1.jpg"];
(4) 设置为高亮状态: imgView.highlighted = YES;  
(5) 设置内容模式
     UIViewContentModeScaleToFill, // 横向和纵向都拉伸到边框
     UIViewContentModeScaleAspectFit, //
等比例拉伸,当某一方向到头时,则不拉伸
     UIViewContentModeScaleAspectFill, //等比例拉伸,当某一方向到头时,则继续拉伸到另一方向也到头
    imgView. contentMode = UIViewContentModeScaleAspectFit ;
(6) 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 = [UIImageimageNamed:@"scene5.jpg"];
    NSArray *images = @[ img1, img2, img3, img4, img5 ] ;
(7)设置动画播放的集合 imgView. animationImages = images;
(8)设置播放动画的时间 imgView. animationDuration = 5 ;
(9)开始动画 [imgView startAnimating];
(10)注意: imageView 默认的触摸事件是关闭的: imgView.userInteractionEnabled = YES;
5.滑块UISlider
slider 显示的高度是固定的
UISlider *slider = [[ UISlider alloc ] initWithFrame : CGRectMake ( 10 , 100 , 300 , 30 )];
(1)设置背景颜色:slider.backgroundColor = [UIColor redColor];
    [ self . window addSubview :slider];
(2)设置滑动的最大值和最小值 slider. maximumValue = 100 ; slider. minimumValue = 0 ;
(3)设置初始值 slider. value = 50 ;
(4)设置滑动条左边 | 右边的颜色
    [slider setMinimumTrackTintColor:[UIColor greenColor]];
    [slider setMaximumTrackTintColor:[UIColor yellowColor]];
(5)设置滑动条的左右边的图片
    UIImage *image1 = [ UIImage imageNamed : @"com_slider_min_l-Decoded" ];
   
UIImage *image2 = [ UIImage imageNamed : @"com_slider_min_r-Decoded" ];
(6)设置图片的拉伸点
    image1 = [image1 stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    image2 = [image2 stretchableImageWithLeftCapWidth:5 topCapHeight:0];
    image1 = [image1resizableImageWithCapInsets:UIEdgeInsetsMake(0,5, 0,5)];
    image2 = [image2 resizableImageWithCapInsets : UIEdgeInsetsMake ( 0 , 5 , 0 , 5 )];
(7)设置滑块状态:
    [slidersetMinimumTrackImage:image1 forState:UIControlStateNormal];
    [slider setMaximumTrackImage :image2 forState : UIControlStateNormal ];
(8)设置滑块的图片
    UIImage *image3 = [ UIImage imageNamed : @"com_thumb_max_n-Decoded" ];
   UIImage *image4 = [UIImageimageNamed:@"com_thumb_max_h-Decoded"];
    [slider setThumbImage :image3 forState : UIControlStateNormal ];
    [slider
setThumbImage :image4 forState : UIControlStateHighlighted ];
(9)设置滑块按钮的颜色 [slider setThumbTintColor:[UIColor redColor]];
(10)添加事件:
    [slideraddTarget:selfaction:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];
6.开关UISwitch
UISwitch *switchUI = [[ UISwitch alloc ] initWithFrame : CGRectMake ( 10 , 200 , 100 , 100 )];
(1) 默认开 switchUI. on = YES ;
(2)添加事件
    [switchUIaddTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];
7.风火轮UIActivityIndicatorView
  UIActivityIndicatorView *activity = [[ UIActivityIndicatorView alloc ] initWithActivityIndicatorStyle : UIActivityIndicatorViewStyleGray ];
    activity.frame =CGRectMake(100,100, 0,0);
(1)开始转动 [activity startAnimating ];
(2)停止转动并且隐藏 [activity stopAnimating];
8.页面控件UIPageControl
UIPageControl *pageControl = [[ UIPageControl alloc ] initWithFrame : CGRectMake ( 100 , 200 , 200 , 20 )];
(1)设置总页数: pageControl. numberOfPages = 5 ;
(2)设置当前选中的页面的索引: pageControl. currentPage = 2 ;
(3)添加点击事件
    [pageControladdTarget:selfaction:@selector(pageAction:)forControlEvents:UIControlEventValueChanged];
9. 进度条UIProgressView
UIProgressView *progress = [[ UIProgressView alloc ] initWithProgressViewStyle : UIProgressViewStyleDefault ];
progress.frame =CGRectMake(10,300, 300,0);
(1)progress. backgroundColor = [ UIColor redColor ];  // 无效
(2)设置进度值: progress.progress = 0.5;
(3)设置已经加载的进度条的颜色: progress. progressTintColor = [ UIColor greenColor ];
(4)设置未加载的进度条的颜色: progress.trackTintColor = [UIColor yellowColor];
10. 提示框AlertViewActionSheet
(1)设置 alertView 的样式
UIAlertViewStyleDefault = 0 ,
UIAlertViewStyleSecureTextInput,   // 默认带了一个输入密码用的 textField
UIAlertViewStylePlainTextInput,    // 默认带了一个明文的 textField
UIAlertViewStyleLoginAndPasswordInput  // 默认带了两个 textField ,用来输入用户名和密码
(2) UIAlertController 代替了 AlertView ActionSheet: UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@" 警告 " message : @" 密码错误 " preferredStyle : UIAlertControllerStyleAlert ];  
11.分段控件UISegmentedControl
UISegmentedControl *segmentCtrl = [[ UISegmentedControl alloc ] initWithItems :array];
segmentCtrl.frame =CGRectMake(20,30, 150,25);
(1)设置样式 segmentCtrl. segmentedControlStyle = UISegmentedControlStyleBar ;
(2)设置默认选中按钮 segmentCtrl.selectedSegmentIndex = 0;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值