那么接下来就是关于UIKit的一些学习了,以下是需要掌握的一些点,框住的是还没彻底掌握的,会继续学习补充。
一、基础空间的介绍
**(一)UIButton **
UIButton:响应用户交互执行自定义代码的控件; 以下是配置按钮的基础操作;
UIButton:响应用户交互执行自定义代码的控件; 以下是配置按钮的基础操作;
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 200, 100, 200);///设置button位置
button.backgroundColor = [UIColor orangeColor];///设置button颜色
[button setTitle:@"登陆" forState:UIControlStateNormal];///添加button的标题
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];///设置标题的颜色
button.titleLabel.font = [UIFont systemFontOfSize:20];///设置标题大小
[button setBackgroundImage:[[UIImage imageNamed:@"11.jpeg"] forState:UIControlStateNormal];///设置button的背景图片
[self.view addSubview:button];
UIButton的使用是比较简单的,可以设置UIButton的文本,背景显示等等。在这里有两个setTitleEdgeInsets和setImageEdgeInsets方法,UIButton在设置图片以及文本时,默认的显示是图片在左,文本在右,一般可以通过这两个方法来进行调整。其实也是挺基础的吧~ 另外就是对UIButton添加响应事件,如下例子:
UIButton *orderbutt = [[UIButton alloc]init];
[orderbutt addTarget:self action:@selector(respondCallMenue:) forControlEvents:UIControlEventTouchUpInside];
只需要实现respondCallMenue就可以,在这里做一些点击按钮之后的操作; 以上都是比较基础的UIButton的使用,当然还有别的属性,可以查看UIButton源码进行理解;
(二)UILable
UILabel:实现只读文本,用该类可绘制一个或者多个静态文本;支持既简单又复杂的样式标签文本,也可控制外观;继承与UIView
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 150, 130,120)];
label1.text = @"this is a test program.";///文本内容
label1.font = [UIFont systemFontOfSize:15];///设置字体大小
label1.textColor = [UIColor redColor];///设置字体颜色
label1.textAlignment = NSTextAlignmentCenter;///设置对齐方式
label1.lineBreakMode = NSLineBreakByWordWrapping;///设置换行方式
label1.shadowColor = [UIColor purpleColor];///设置阴影颜色
label1.shadowOffset = CGSizeMake(0,-2);///设置阴影的偏移量,默认的事CGSizemake(0,-1):
label1.numberOfLines = 0;///设置行数(当文本需要自适应时,需要将行数设置为0);
label1.adjustsFontSizeToFitWidth = YES;
[label1 setHighlighted:YES];///高亮属性
label1.highlightedTextColor = [UIColor blueColor];///设置高亮状态的字体
label1.userInteractionEnabled = YES;///设置能否进行用户交互,默认为NO
label1.enabled = NO;//此属性值决定如何绘制标签,禁用的文本有些模糊,表示他不活跃。默认为YES;
label1.adjustsFontSizeToFitWidth = YES;///设置字体大小自适应label宽度;
[self.view addSubview:label1];
UIlable就是一个文本控件,一般用来展示一些静态文本,同时也能够根据文本字体大小设置自适应宽高;
(三)UITextField
UITextField:在界面中显示可编辑文本区域的对象 @interface UITextField:UIControl
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 100, 200,50)];///初始化一个UITextfield控件;
textField.placeholder = @"请输入文字";///设置占位文本
textField.textColor = [UIColor blackColor];///设置文本颜色
textField.font = [UIFont systemFontOfSize:14];///设置文本大小
textField.textAlignment = NSTextAlignmentLeft;///设置对齐方式
///[textField setEnabled:NO];///输入框不能编辑
///textField.secureTextEntry = YES;///设置编辑框中的内容密码显示
textField.background = [UIImage imageNamed:@"登陆logo"];///启用文本字段时显示的背景图像。该图像显示在文本字段内容的其余部分后面
textField.borderStyle = UITextBorderStyleRoundedRect;///设置边框样式,默认为UITextBorderStyleNone
textField.clearButtonMode = UITextFieldViewModeUnlessEditing;///设置清楚按钮的模式
textField.minimumFontSize = 14;///文本字段的最小字体大小;当”调整为合适“选项启用时,文本字段会自动更改字体大小以确保文本的最大可读性。
textField.keyboardType = UIKeyboardTypePhonePad;///设置键盘类型
textField.returnKeyType = UIReturnKeyJoin;///设置键盘上返回键的类型
textField.keyboardAppearance = UIKeyboardAppearanceLight;///设置键盘的视觉样式
textField.spellCheckingType = UITextSpellCheckingTypeNo;///文本字段的拼写检查行为,此属性决定了拼写检查在打字过程中是启用还是禁用;
[self.view addSubview:textField];
UITextField是一个很重要的控件,他的使用场景很多,比如登录注册时作为账号密码的输入框,搜索时作为搜索的输入框等等。以上的例子都是比较基础的操作,需要注意的点:一是文本框内占位符的样式,二是关于键盘的相关显示等等。这个后期会继续深入学习与使用!!!!!!
(四)UIImageView和UIImage
UIImage:管理图像数据的对象
通过一下几种不同的方式使用图像:
- 将图像分配给对象以在界面中显示该对象;
- 使用图像可自定义系统控件,例如按钮,滑块和分段控件;
- 将图像直接绘制带视图或其他徒刑上下文中;
- 将图像传递到可能需要图像数据的其他API;
UIImageView:在界面显示单个图像或一系列动画图像的对象 一般配合UIImage一起使用, UIImage加载图片,UIImageView将其加载的图片显示到视图界面中;
UIImage *image = [UIImage imageNamed:@"Image"];///加载图片,定义并初始化image
UIImageView *imageview = [[UIImageView alloc]init];///使用UIImageView将图片加载到视图上
imageview.frame = CGRectMake(0, 100,405, 205);///设置视图的大小,四个参数:左起点。上起点。图片的长。 宽
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,205,905)];
[imageview setContentMode:UIViewContentModeCenter];///参数为UIViewContentMode枚举类型 UIViewContentMode的参数用来调整图片的显示位置
UIViewContentMode的参数用来调整图片的显示位置 UIImageView一般配合UIImage一起使用,用来显示图像;
(五)UITextView
UITextView:可滚动的多行文本区域。 @interface UITextView : UIScrollView 继承于UIScrollView
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 80, 200,200)];
textView.text = @"this is a test program!his is a test program!his is a test program!his is a test program!his is a test program!his is a test program!his is a test program!his is a test program!his is a test program!";
textView.textColor = [UIColor blackColor];
textView.font = [UIFont systemFontOfSize:30.f];
textView.textAlignment = NSTextAlignmentCenter;
//textView.selectedRange = NSMakeRange(8,6);
///富文本
/*
NSMutableAttributedString *attrstr = [[NSMutableAttributedString alloc]initWithString:@"这是一个富文本"];
[attrstr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize: 30.f]
range:NSMakeRange(4, 3)];
textView.attributedText = attrstr;
textView.allowsEditingTextAttributes = NO;///设置是否允许改变文本属性字典。默认时YES
*/
NSMutableDictionary * attributesDic = [textView.typingAttributes mutableCopy];
[attributesDic setObject:[UIColor redColor] forKey:NSForegroundColorAttributeName];
// automatically resets when the selection changes
// 重新设置 接下来改变的文字 的属性字典
textView.typingAttributes = attributesDic;
一般在一些代理函数中使用,比如当编辑状态的变化
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 100, 100)];
view.backgroundColor = [UIColor redColor];
textView.inputView = view;
textView.inputAccessoryView = view;///输入键盘加视图
UITextView的使用区别于UITextField,UITextField的使用一般用来显示单行文本,UITextView一般作用显示多行可滚动的文本区域;
(六)UIToolbar
UIToolbar:视图的属性,可以在工具栏上添加按钮Bar Button Item(可以是自定义的Custom,也可以是系统自带的BarButtonSystem),视图控制器可以通过工具栏项对视图内容进行操作;
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 667-60, 375, 40)];///创建toolbar导航栏实例
toolbar.barStyle = UIBarStyleBlack;///设置toolbar风格
[self.view addSubview:toolbar];///将其添加到视图中
UIBarButtonItem *additem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:nil];///创建空间上的按钮。 增加按钮
UIBarButtonItem *editem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:nil];///编辑按钮
UIBarButtonItem *titleitem = [[UIBarButtonItem alloc]initWithTitle:@"tile" style:UIBarButtonSystemItemDone target:self action:nil];///设置按钮
UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:self action:nil ];///设置按钮间距
NSArray *items = @[additem,flexibleitem,editem,flexibleitem,titleitem];///将所有的按钮放在数组中
[toolbar setItems:items animated:YES];///设置导航栏上的按钮单元
以上是几种基础控件的简单介绍,当然在使用的过程种并不止如此,根据不同的需求采用相应的方式方法来完成。查看源码以及官方文档是个很好的方式。