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

本文深入探讨iOS应用开发中的Swift编程语言,分享了Swift语言的基础语法、高级特性以及实战经验,包括数据类型、控制结构、函数式编程、错误处理、泛型、协程等内容,旨在帮助开发者提高开发效率并解决实际开发过程中的常见问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.文本UILabel
    UILabel *textLable = [[UILabelalloc]initWithFrame:CGRectMake(10,30,150,250)];
(1)设置文本内容textLable.text= @"good morning hehehehehe good morning hehehehehe";
(2)设置字体, systemFont使用系统的字体:textLable.font= [UIFontsystemFontOfSize:16];
(3)设置粗体textLable.font = [UIFont boldSystemFontOfSize:16];
(4)设置字体颜色textLable.textColor= [UIColororangeColor];
(5)设置文本对齐方式textLable.textAlignment= NSTextAlignmentCenter;
(6)设置当前的显示行数,默认是1,如果设为0,是自动换行:textLable.numberOfLines= 0;
(7)自动根据文本调整宽度和高度[textLablesizeToFit];
2.按钮UIButton
UIButton*button = [UIButtonbuttonWithType: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= [UIFontboldSystemFontOfSize:20];
(6)设置标题的颜色[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
(7)获取图片  imageNamed:在当前工程目录下找到名字为“”的图片,如果图片是png格式的,可以不加.png,但是建议还是添加上:
UIImage*image1 = [UIImageimageNamed:@"back_on_black.png"];
UIImage*image2 = [UIImageimageNamed:@"back_on.png"];
(8)设置背景图片-----图片会随着按钮的frame改变而拉伸[button setBackgroundImage:image1 forState:UIControlStateNormal];
(9)设置高亮状态下的图片[button setBackgroundImage:image2 forState:UIControlStateHighlighted];
(10)设置图片-----图片不会被拉伸--但是imageView上面不能显示Title
[buttonsetImage:image1forState:UIControlStateNormal];
[buttonsetImage:image2forState:UIControlStateHighlighted];
(11)给按钮添加点击事件[buttonaddTarget:selfaction:@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 = [[UITextFieldalloc]initWithFrame:CGRectMake(100,100,200,30)];
(1)设置标签值:textField.tag =100;
(2)设置输入框的边框样式,默认无边框textField.borderStyle= UITextBorderStyleRoundedRect;
(3)设置输入文字的字体textField.font= [UIFontboldSystemFontOfSize:18];
(4)设置文字的颜色textField.textColor= [UIColorredColor];
(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 = [[UIImageViewalloc]initWithFrame:CGRectMake(0,100,300,100)];
(1)设置背景颜色imgView.backgroundColor= [UIColorredColor];
(2)设置背景图片:UIImage *img = [UIImageimageNamed:@"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 = [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];
(7)设置动画播放的集合imgView.animationImages= images;
(8)设置播放动画的时间imgView.animationDuration= 5;
(9)开始动画[imgView startAnimating];
(10)注意:imageView默认的触摸事件是关闭的:imgView.userInteractionEnabled = YES;
5.滑块UISlider
slider显示的高度是固定的
UISlider*slider = [[UISlideralloc]initWithFrame:CGRectMake(10,100,300,30)];
(1)设置背景颜色:slider.backgroundColor = [UIColor redColor];
    [self.windowaddSubview:slider];
(2)设置滑动的最大值和最小值slider.maximumValue= 100;slider.minimumValue= 0;
(3)设置初始值slider.value= 50;
(4)设置滑动条左边|右边的颜色
    [slider setMinimumTrackTintColor:[UIColor greenColor]];
    [slider setMaximumTrackTintColor:[UIColor yellowColor]];
(5)设置滑动条的左右边的图片
   UIImage *image1 = [UIImageimageNamed:@"com_slider_min_l-Decoded"];
   
UIImage *image2 = [UIImageimageNamed:@"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 = [image2resizableImageWithCapInsets:UIEdgeInsetsMake(0,5,0,5)];
(7)设置滑块状态:
    [slidersetMinimumTrackImage:image1 forState:UIControlStateNormal];
    [slidersetMaximumTrackImage:image2forState:UIControlStateNormal];
(8)设置滑块的图片
   UIImage *image3 = [UIImageimageNamed:@"com_thumb_max_n-Decoded"];
   UIImage *image4 = [UIImageimageNamed:@"com_thumb_max_h-Decoded"];
    [slidersetThumbImage:image3forState:UIControlStateNormal];
    [slider
setThumbImage:image4forState:UIControlStateHighlighted];
(9)设置滑块按钮的颜色[slider setThumbTintColor:[UIColor redColor]];
(10)添加事件:
    [slideraddTarget:selfaction:@selector(sliderAction:)forControlEvents:UIControlEventValueChanged];
6.开关UISwitch
UISwitch*switchUI = [[UISwitchalloc]initWithFrame:CGRectMake(10,200,100,100)];
(1)默认开switchUI.on= YES;
(2)添加事件
    [switchUIaddTarget:selfaction:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];
7.风火轮UIActivityIndicatorView
 UIActivityIndicatorView*activity = [[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activity.frame =CGRectMake(100,100, 0,0);
(1)开始转动[activitystartAnimating];
(2)停止转动并且隐藏[activity stopAnimating];
8.页面控件UIPageControl
UIPageControl*pageControl = [[UIPageControlalloc]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 = [[UIProgressViewalloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
progress.frame =CGRectMake(10,300, 300,0);
(1)progress.backgroundColor= [UIColorredColor]; //无效
(2)设置进度值:progress.progress = 0.5;
(3)设置已经加载的进度条的颜色:progress.progressTintColor= [UIColorgreenColor];
(4)设置未加载的进度条的颜色:progress.trackTintColor = [UIColor yellowColor];
10.提示框AlertViewActionSheet
(1)设置alertView的样式
UIAlertViewStyleDefault =0,
UIAlertViewStyleSecureTextInput,  //默认带了一个输入密码用的textField
UIAlertViewStylePlainTextInput,   //默认带了一个明文的textField
UIAlertViewStyleLoginAndPasswordInput //默认带了两个textField,用来输入用户名和密码
(2)UIAlertController代替了AlertViewActionSheet:UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"警告"message:@"密码错误"preferredStyle:UIAlertControllerStyleAlert]; 
11.分段控件UISegmentedControl
UISegmentedControl*segmentCtrl = [[UISegmentedControlalloc]initWithItems:array];
segmentCtrl.frame =CGRectMake(20,30, 150,25);
(1)设置样式segmentCtrl.segmentedControlStyle= UISegmentedControlStyleBar;
(2)设置默认选中按钮segmentCtrl.selectedSegmentIndex = 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值