一、UIButton 按钮
类型
UIButtonTypeRoundedRect, 圆角矩形
UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
UIButtonTypeInfoLight, 亮色感叹号
UIButtonTypeInfoDark, 暗色感叹号
UIButtonTypeContactAdd, 十字加号按钮
状态
UIControlStateNormal 常态
UIControlStateSelected 选中状态
创建
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(50, 200, 50, 25)];
设置属性
Button在常态下的
标题
[btn setTitle:@”常态” forState:UIControlStateNormal];
颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
图片
[btn setImage:[UIImage imageNamed:@”checkbox1_unchecked”] forState:UIControlStateNormal];
背景颜色
[btn setBackgroundColor:[UIColor purpleColor]];
背景图片
[btn setBackgroundImage:[UIImage imageNamed:@”img2”] forState:UIControlStateSelected];
绑定事件
[btn addTarget:self action:@selector(dianji) forControlEvents:UIControlEventTouchUpInside];
设置按钮标题字体大小
[btn.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
设置按钮按下会发光
btn.showsTouchWhenHighlighted=NO;
获取当前状态下按钮的文字
btn.currentTitle
设置按钮内部图片间距和标题间距
内部图片间距
UIEdgeInsets insets;
insets.top/bottom//left/right = 10;
btn.contentEdgeInsets = insets;
标题间距
btn.titleEdgeInsets = insets;
设置按钮尺寸跟背景图片一样大
[btn sizeToFit];
设置内边距
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
处理图片和标题
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
return CGRectMake(4, 3, 20, 20);
}
(CGRect)titleRectForContentRect:(CGRect)contentRect
{
return CGRectMake(30, 0, 60, 25);
}
二、UILabel 文本标签
文字大小
lbl.font=[UIFont systemFontOfSize:40];
指定字体样式和大小
lbl.font = [UIFont fontWithName:@”Arial” size:16];
加粗字体
lbl.font = [UIFont boldSystemFontOfSize:15];
自定义字体
lbl.font = [UIFont fontWithName:@”Helvetica Neue” size:17];
显示多行
lbl.numberOfLines=0;
行数
lbl.numberOfLines = 2;
自动折行
lbl.lineBreakMode = UILineBreakModeWordWrap;
设置对齐方式
居中对齐
lbl.textAlignment=UITextAlignmentCenter;
居左对齐 UITextAlignmentLeft
居右对齐 UITextAlignmentRight
文字颜色
lbl.textColor=[UIColor purpleColor];
背景颜色
lbl.backgroundColor=[UIColor whiteColor];
阴影效果
lbl.shadowColor=[UIColor redColor];
阴影偏移量
lbl.shadowOffset=CGSizeMake(7, 7);
文本省略方式
省略中间的文字
self.Biaoqiankongjian.lineBreakMode=NSLineBreakByTruncatingMiddle;
省略开头的文字 NSLineBreakByTruncatingHead
省略结尾的文字(默认) NSLineBreakByTruncatingTail
设置行为
可用
lbl.enabled=YES;
高亮
lbl.highlighted=YES;
把Text控件中的文本转到Label中
lbl.text=self.shur.text;
字体大小适应label宽度
lbl.adjustsFontSizeToFitWidth = YES;
lbl.minimumScaleFactor = 0.3;
lbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
三、UITextField 文本输入框
设置边框样式
圆角边框
[txt setBorderStyle:UITextBorderStyleRoundedRect];
无边框
细边框
3D边框
密码框
txt.secureTextEntry = YES;
水印提示内容
txt.placeholder = @”提示内容”;
自动缩小字体大小以适应文本窗口大小
txt.adjustsFontSizeToFitWidth = YES;
清空按钮(叉号)
txt.clearButtonMode = UITextFieldViewModeWhileEditing;
自动缩小显示的最小字体大小
text.minimumFontSize = 14;
设置文本框的输出视图(虚拟键盘)
txt.inputView = v;
设置键盘的样式
text.keyboardType = UIKeyboardTypeNumberPad;
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
return键(右下角按钮)样式
txt.returnKeyType =UIReturnKeyDone;
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
设置文本框的左右视图
txt.leftView = imgView;
txt.leftViewMode = UITextFieldViewModeAlways;
txt.rightView = btn;
txt.rightViewMode = UITextFieldViewModeWhileEditing;
隐藏虚拟键盘
1.失去第一响应者 [txt resignFirstResponder];
2.方法touchBegan失去第一响应者 [txt resignFirstResponder];
3.方法DidEndOnExit,失去第一响应者 [txt resignFirstResponder];
文本框得到焦点,界面出现弹出虚拟键盘
[txt becomeFirstResponder];
键盘样式
暗
txt.keyboardAppearance = UIKeyboardAppearanceDark
亮
UIKeyboardAppearanceLight
设置文本框(虚拟键盘)的工具栏
txt.inputAccessoryView = tool;
UITextFeildDelegate协议
1.文本框开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField
2.文本框结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField
3.文本框的值将要发生改变时
- (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string
4.禁用文本框复制粘贴功能
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
四、UIImageView 图片视图
设置图片
1.UIImage *img1 = [UIImage imageNamed:@”mtxx3.png”]; (小于5k)
2.UIImage *img2 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@”mtxx4” ofType:@”png”]];
imgView.image = img2;
3.NSData *data=[NSData dataWithContentsOfFile:filePath];
UIImage *img3=[UIImage imageWithData:data];
[imgView setImage:img3];
为图片添加单击事件
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer:singleTap];
播放图片
imgView.animationDuration = 1.0;时间
imgView.animationRepeatCount = 0;次数
imgView.animationImages = @[img1,img2,img3];图片数组
开始播放
[imgView startAnimating];
结束播放
[imgView stopAnimating];
imageView.animationImages = imagesArray;
设定所有的图片在多少秒内播放完
imageView.animationDuration = [imagesArray count];
0表示无数遍
imageView.animationRepeatCount = 0;
开始播放
[imageView startAnimating];
缩放图像
将原来的宽度和高度缩放到w、h倍
imageView.transform = CGAffineTransformMakeScale(w , h);
旋转图像
顺时针方向旋转,旋转中心是原始ImageView的中心(center表示的位置)
imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);
设图片置内容模式
UIViewContentModeScaleToFill 填充
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
imageV.contentMode = UIViewContentModeScaleAspectFill;
裁剪图片,超出控件的部分裁剪掉
imageV.clipsToBounds = YES;
五、UITableView 表格视图
1.UITableView ***************
设置背景图片
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@”mtxx68” ofType:@”png”]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
imgView.bounds = _tbView.bounds;
_tbView.backgroundView = imgView;
设置分割线
_tbView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_tbView.separatorColor = [UIColor greenColor];
2.UITableViewCell 表格单元格 **********
创建
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@”“];
Style 样式
UITableViewCellStyleDefault (textLabel+imageView)
UITableViewCellStyleValue1 (textLabel+detailTextLabel+imageView) 左右
UITableViewCellStyleValue2 (textLabel+detailTextLabel)
UITableViewCellStyleSubtitle (textLabel+detailTextLabel+imageView) 上下
设置Cell的按钮样式
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;(向右的箭头)
UITableViewCellAccessoryNone,
UITableViewCellAccessoryDetailDisclosureButton,
UITableViewCellAccessoryCheckmark,
UITableViewCellAccessoryDetailButton
设置选中行的样式
无
cell.selectionStyle = UITableViewCellSelectionStyleNone;
灰色
UITableViewCellSelectionStyleGray
设置cell文字显示的
字体大小
cell.textLabel.font=[UIFont systemFontOfSize:16];
行数
cell.textLabel.numberOfLines=2;
颜色
cell.textLabel.textColor=[UIColor xxxx];
阴影
cell.textLabel.shadowColor = [UIColor whiteColor];
对齐方式
cell.textLabel.textAlignment=NSTextAlignmentCenter;
给cell设置tag值
cell.textLabel.tag=10;
改变Cell选中时背景色(背景图片同理)
通过RGB来定义自己的颜色
UIColor *color = [[UIColoralloc]initWithRed:0.0green:0.0blue:0.0alpha:1];
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];
取消Cell选中的功能,在viewWillAppear方法中加入:
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
创建NSIndexPath对象
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
根据sender得到它所在TableView的IndexPth
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
********************3.UITableView协议***************
主要
1).提供表格有多少个分组(默认是一个分组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2).提供分组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3).提供单元格内容
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
4).选中行事件
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
5).提供表格单元格的行高(默认高度:44.0)
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
其他
6).提供表格编辑的样式(默认是删除样式)
- (UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath{
/*
UITableViewCellEditingStyleNone, 无
UITableViewCellEditingStyleDelete, 删除
UITableViewCellEditingStyleInsert 插入
UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert 多选
*/
return _editingStyle;
}
7).提供分组的头文本标题
- (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
8).提供分组的尾文本标题
- (NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section
9).提供分组的头视图
- (UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section
10).提供分组的尾视图
- (UIView )tableView:(UITableView )tableView viewForFooterInSection:(NSInteger)section
六、UIScrollView和UIPageControl 滚动视图和分页控件
设置内容大小(滚动条件:内容大小>尺寸大小)
_sclView.contentSize = imgView.bounds.size;
得到内容的偏移量
CGPoint point = scrollView.contentOffset;
滚动条样式
_sclView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
是否显示滚动条
_sclView.showsHorizontalScrollIndicator = NO;//横向
_sclView.showsVerticalScrollIndicator = NO;//纵向
整页滚动
_sclView.pagingEnabled = YES;
是否可缩放
_sclView.scrollEnabled = YES;
是否有回弹效果
_sclView2.bounces = NO;
取消触摸
self.scrollView.canCancelContentTouches = NO;
滚动条类型
self.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
是否自动裁切超出部分
self.scrollView.clipsToBounds = YES;
是否可以进行画面切换
self.scrollView.pagingEnabled = YES;
UIScrollViewDelegate协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 滚动视图正在滚动
UIPageControl 分页控件
分页个数
pgCtrl.numberOfPages = _imgs.count;
设置颜色
其他
pgCtrl.pageIndicatorTintColor = [UIColor blueColor];
当前
pgCtrl.currentPageIndicatorTintColor = [UIColor redColor];
七、UIAlertView和UIActionSheet 警告视图和动作表单
UIAlertView 警告视图(中间弹框)
创建
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”提示” message:@”你确定吗?” delegate:nil cancelButtonTitle:@”取消” otherButtonTitles:@”确定”,@”非常确定”, nil];
显示
[alert show];
样式
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
UIAlertViewStyleDefault = 0, 默认
UIAlertViewStyleSecureTextInput, 密码框
UIAlertViewStylePlainTextInput, 文本框
UIAlertViewStyleLoginAndPasswordInput 文本和密码框
设置自动消失
[alt dismissWithClickedButtonIndex:0 animated:YES];
UIAlertViewDelegate 协议
获取点击按钮的下标
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
警告视图将要弹出时
- (void)willPresentAlertView:(UIAlertView *)alertView
更改AlertView背景
UIImage *theImage = [UIImageimageNamed:@”loveChina.png”];
theImage = [theImage stretchableImageWithLeftCapWidth:0topCapHeight:0];
CGSize theSize = [theAlert frame].size;
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(5, 5, theSize.width-10, theSize.height-20)];//这个地方的大小要自己调整,以适应alertview的背景颜色的大小。
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
theAlert.layer.contents = (id)[theImage CGImage];
UIActionSheet 动作表单(底部弹框)
创建
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@”请选择” delegate:self cancelButtonTitle:@”取消” destructiveButtonTitle:@”中国” otherButtonTitles:@”韩国”,@”日本”, nil];
[action showInView:self.view];
UIActionSheet协议
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex)
}
八、UICollectionView 集合视图
组成
界面组成:Cells(用于展示内容的主体)+Supplementary Views 追加视图 (相当于tableView的Header或者Footer) +Decoration Views 装饰视图(每个section的背景)
数据组成:UICollectionViewDataSource+UICollectionViewLayout+UICollectionViewDelegate(交互部分)
用法:
一、UICollectionViewDataSource
1.注册并创建图片数组
[self.collectionView registerClass:[CYChangeIconCell class] forCellWithReuseIdentifier:@”Cell”];
NSMutableArray *_iconArray=[[NSMutableArray alloc]initWithCapacity:10];
for (int i=0; i<8; i++) {
NSString *imgName=[NSString stringWithFormat:@”icon%i.jpg”,i+1];
[_iconArray addObject:imgName];
}
2.section里有多少个item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [_imgArray count];
}
3.某个位置应该显示什么样的cell
-(UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *iden=@"Cell";
CYChangeIconCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:iden forIndexPath:indexPath];
NSString*img=[_iconArray objectAtIndex:indexPath.row];
cell.imgView.image=[UIImage imageNamed:img];
return cell;
}
4.选中item
-(void)collectionView:(UICollectionView )collectionView didSelectItemAtIndexPath:(NSIndexPath )indexPath{
NSString*img=[_iconArray objectAtIndex:indexPath.row];
[self.delegate sendImg:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:img ofType:nil]]];
[self dismissViewControllerAnimated:YES completion:nil];
}
5section的数量
-numberOfSectionsInCollection:
6.其他
-collectionView:shouldHighlightItemAtIndexPath: 是否高亮
-collectionView:didHighlightItemAtIndexPath: 1是高亮
-collectionView:didUnhighlightItemAtIndexPath: 1是取消高亮
-collectionView:shouldSelectItemAtIndexPath: 是否可以被选中
-collectionView:didSelectItemAtIndexPath: 3是选中cell
Header和Footer尺寸
-collectionView:layout:referenceSizeForHeaderInSection:
-collectionView:layout:referenceSizeForFooterInSection:
二、UICollectionViewLayout
设置滚动方向
layout.scrollDirection=
UICollectionViewScrollDirectionVertical
UICollectionViewScrollDirectionHorizontal
清空行距
layout.minimuLineSpacing=0;
三、创建集合视图
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
layout.itemSize=CGSizeMake(77.0, 77.0);
CYChangeIconController *cic=[[CYChangeIconController alloc] initWithCollectionViewLayout:layout];
cic.delegate=self;
[self presentViewController:cic animated:YES completion:nil];
九、UISwtich、UISegmentedControl、UISlider 开关、分段控件和滑块
****************1.UISwtich*****************
State 状态
On 开
Off 关
On Tint 开关开启的颜色
ison 判断开关是否开启
setOn:animation: 启动开关,是否加动画
**************2.UISegmentedControl*************
Segments 分段个数
Title 标题
Image 图片
Behavior(Eabled 是否可用;Selected 默认是否选中)
Content Offset 内容的偏移量
style 样式
Plain 无边框
Borders 细边框
Bar (用于导航栏)
state Momentary 选中项是否还原
得到选中的分段控件的下标
NSInteger index = segCtl.selectedSegmentIndex;
根据下标得到内容
NSString *title = [segCtl titleForSegmentAtIndex:index];
UIImage *img = [segCtl imageForSegmentAtIndex:index];
**************3.UISlider*************
1.Value(0-1)
Minimum 最小值
Maximum 最大值
Current 当前的值
slider.minimumValue = 0.0;
slider.maximumValue = 100.0;
slider.value = 10.0;
2.Min Image
Max Image
//设置内容图片
[slider setMinimumTrackImage:[UIImage imageNamed:@”slider1.png”] forState:UIControlStateNormal];
[slider setMaximumTrackImage:[UIImage imageNamed:@”slider2.png”] forState:UIControlStateNormal];
//添加滑块的事件(ValueChange)
[slider addTarget:self action:@selector(slidervaluechange:) forControlEvents:UIControlEventValueChanged];
十、UIPickerView 选择器视图
UIPickerViewDataSource,UIPickerViewDelegate 协议
提供选择器有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
提供每一列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
提供文本内容
- (NSString )pickerView:(UIPickerView )pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
提供视图内容
- (UIView )pickerView:(UIPickerView )pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
提供行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 200;
}
提供列的长度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 100;
}
得到选中行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
手动设置指定行选中
[pv selectRow:1 inComponent:0 animated:YES];
刷新
[pickerView reloadComponent:1];刷新指定的列
[pickerView reloadAllComponents];刷新所有的列
十一、UISearchBar和UIRefreshControl 搜索框和刷新控件
UISearchBar
UISearchDisplayController 搜索控制器
组成:UISearchBar+UITableViewController
NSPredecate 谓词
UIRefreshControl
设置提示信息
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@”下拉刷新”];
开始加载
[self.refreshControl beginRefreshing];
结束加载
[self.refreshControl endRefreshing];
十二、UIProgressView和UIActivityIndicatorView 进度条和活动指示器
UIProgressView
1.Styel
Default
Bar
//样式
prgView.progressViewStyle = UIProgressViewStyleDefault;
//
prgView.progressImage = [UIImage imageNamed:@”progress1.png”];
prgView.trackImage = [UIImage imageNamed:@”progress2.png”];
2.progress
值范围:0-1
UIActivityIndicatorView
设置样式
大白
aiv.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
Gray 小灰
White 小白
颜色
aiv.tintColor = [UIColor cyanColor];
行为
Animating 是否转动
[aiv startAnimating];
[aiv stopAnimating];
停止转动时是否隐藏
aiv.hidesWhenStopped = YES;
UIActivityIndicatorView
设置网络活动指示器