控件综合属性总结
坐标相关---------------------------
CGRect frame = CGRectMake(280, 12, 20, 20);
CGSize size=CGSizeMake(320,420);
CGPoint point=CGPointMake(30,60);
CGRect rect=[[UIScreen mainScreen] applicationFrame];
//获得屏幕可显示内容的最大显示区域(0,20,320,460)
//CGRect是结构体类型,不要用%@打印
CGRect screenRect=[self.view bounds];// 整个屏幕大小CGRect rect=self.view.bounds; CGSize
lineBreakMode:UILineBreakModeWordWrap];
//切页经典函数,根据给定的字体和大小范围,返回一个真实的大小
UIFont相关-----------------------
UIFont* font=[UIFont fontWithName:@"Arial" size:40.0f];//字体格式
UIFont *font = [UIFont systemFontOfSize:13];
NSArray*
label.text=(NSString*)font;
CGFloat oneLineHeigth=[@"a我W" sizeWithFont:font].height;
//取得一行高度,siezWithFont 返回的是一个CGSIZE类型
UILabel相关-----------------------
UILabel* label=[[UILabel alloc]
initWithFrame:CGRectMake(0, 0, realSize.width, realSize.height)];
UILabel* label=[[UILabel alloc] initWithFrame:CGRectZero];//不指定大小的初始化
label.numberOfLines=0;//设置最大行数,超过指定行则截断用省略号表示 ,默认为1,为0 表示不限制
label.lineBreakMode=UILineBreakModeCharacter
label.text=str;
label.textColor=[UIColor blueColor];
lable.textAlignment=UITextAlignmentCenter;//设置字体对齐方式
label.backgroundColor=[UIColor clearColor]; //设置label背景色
label.font=font;
label.frame=CGRectMake(0,0,1,1);
self.contentLabel=label;
int val=[label.text intValue];//NSString 到 intValue的转换
UILabel* label=(UILabel*)[self.view viewWithTag:100];
//每一个viewController 都有一个self.view,viewWithTag:100找出该
view上tag值为100的控件,返回值为UIView*类型
[self.view addSubview:self.contentLabel];
[label addSubview:img];
UIView相关--------------------
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(3, 3, 300, 40)];
view.backgroundColor=[UIColor clearColor];
for(UIView *subview in [cell.contentView subviews])//获取view的子视图
[subview removeFromSuperview];//从父视图中移除子视图
UIButton相关------------------------
UIButton *button;
button.frame=CGRectMake(280, 12, 20, 20);
button.tag=123
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"Pre" forState:UIControlStateNormal];
//UIButton设置标题 不是通过label.text而是用setTitle方法。很容易出错
button.alpha=0.5f;//设置透明
[button setBackgroundImage:[UIImageimageNamed:@"title.png"]
forState:UIControlStateNormal];//给buttton设置背景图片
addTarget:self action:@selector(headerClick:)
forControlEvents:UIControlEventTouchUpIns
//给控件手动增加响应动作
UITabBarItem----------------------
UITabBarItem *tabBar=[[UITabBarItem alloc]
initWithTitle:@"机票查询"
image:[UIImage imageNamed:@"near.png"]
tag:4];
UITabBarItem *tabBar5=[[UITabBarItem alloc]
initWithTabBarSystemItem
tag:3];
//用系统给定的方式初始化initWithTabBarSystemItem
UITabBarController -----------------------------
UITabBarController *tabBarController
tabBarController.viewControllers=allTabs;//allTabs为包含viewController的数组
tabBarController.selectedIndex=2
UISearchBar相关-------------------
UISearchBar *sb=[[UISearchBar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];//设置搜索框
self.searchBar.keyboardType=UIKeyboardTypeAlphabet;//键盘样式设置
self.searchBar.text
UIColor相关------------------------------
UIColor *color=[[UIColor alloc]
initWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];
//UIImage到UIColor的转换,用图像做背景的很重要步骤
UIColor *color=[UIColor
colorWithRed:111/255
UIColor *color=[UIColor clearColor]//透明色
读取文件------------------------------
NSString* path=[[NSBundle mainBundle]
pathForResource:@"1" ofType:@"txt"];
NSError* error;
NSString* contenString=[NSString
stringWithContentsOfFile
encoding:NSUTF8StringEncoding
error:&error];
if (nil==contenString) {
NSLog(@"error:%@", error);
return nil;
}
return contenString;
//读取plist xml文件
NSString *dataPath = [[NSBundle mainBundle]
pathForResource:@"Data" ofType:@"plist"];
self.data = [NSArray arrayWithContentsOfFile:dataPath];
代理相关------------------------
self.myTableView.delegate=self;
self.myTableView.dataSource=self;
if([delegate respondsToSelector:@selector(foo:withValue:)])
//判断delegate中是否实现了foo函数
self.searchDc.searchResultsDelegate=self;
self.searchDc.searchResultsDataSource=self;
[ptr performSelector:selPrintHello];
对话框相关-----------------------
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"你按下的按钮的index为%d", buttonIndex);
//可以在此添加对话框触发的事件
}
UIAlertView* av=[[UIAlertView alloc]
initWithTitle:@"信息"
message:msg
delegate:self cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", @"知道了",nil];
//点击所弹出的对话框
[av show];
[av release];
UITextField相关-------------------------
UITextField * textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];
1.
2.
3.
4.
5.
6.
7.
8.
9.
10. textFieldRounded.delegate = self;
UITextField* nameTextField;
UITextField* ageTextField;
-(BOOL)textFieldShouldReturn:(UITextField *)textField//点击return后执行此函数
{
[self.nameTextField resignFirstResponder];
//让它失去第一响应者,从而点击此按键后键盘消失并执行函数中的代码
NSLog(@"return key down");
return YES;
}
-(IBAction)touchView:(id)sender
{
NSLog(@"touchView");
[self.nameTextField resignFirstResponder];
[self.ageTextField resignFirstResponder];
}
textField.text=@"";
UIPickerView相关--------------------------
-(NSInteger)numberOfComponentsInPick
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
-(CGFloat)pickerView:(UIPickerView *)pickerView
widthForComponent:(NSInteger)component
//返回每一列的宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component
//设置每一行的高度
-(UIView*)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
//返回具体某列某行的视图,在此函数中设置具体列中具体行
-(void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component
// 当选众某单元格时调用的函数
int row0=[pickerView selectedRowInComponent:0];
//获取 在哪个component中选中的哪行
UIView* view0=[pickerView viewForRow:row0
//在component中的row中的view
UILabel* label0=[view0 viewWithTag:200];
//找出view中tag值为200的label
UIDatePicker相关--------------------
UIDatePicker* datePicker;
[self.datePicker
addTarget:self
action:@selector(dateChanged:) forControlEvents:UIControlEventValueChang
//点击datepicker所触发事件
if(self.datePicker.superview) //datePicker有父视图说明此视图才显示
[self.datePicker removeFromSuperview];//将视图从其父视图移除
NSDateFormatter* dateFormatter=[[NSDateFormatter alloc]init];
//日期显示方式
[dateFormatter setDateStyle:NSDateFormatterShortStyl
[dateFormatter setTimeStyle:NSDateFormatterShortStyl
dateLabel.text=[dateFormatter stringFromDate:self.datePicker.date];
//在label上以NSDateFormatter显示
UISlider相关------------------
UISlider* slider=(UISlider*)sender;
int val=[slider value];//取出UISlider的值
UISearchDisplayControlle
UISearchDisplayControlle
self.searchDc=[[[UISearchDisplayControlle
initWithSearchBar:self.searchBar contentsController:self] autorelease];
//初始化并关联searchBar和当前的UIViewControl
UIImageView相关-------------------------------------
UIImage *img1=[UIImage imageNamed:me? @"bubbleSelf.png":@"bubble.png"];
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 30, 30)];
imgView.image=[UIImage imageNamed:@"right.png"];
UIImageView *imgView=[[UIImageView alloc]
initWithImage: [img stretchableImageWithLeft
topCapHeight:14]];
//img stretchableImageWithLeft
imgView.backgroundColor=[UIColor clearColor];
cell设置相关--------------------------
cell.tag
cell.textLabel.text=str;
cell.textLabel.textAlignment = UITextAlignmentRight
cell.accessoryType=UITableViewCellAccessory
//设置cell访问的类型显示向右的箭头//accessory--附件Indicator--指示
cell.selectionStyle=UITableViewCellSelection
cell.selectionStyle=UITableViewCellSelection
UITableViewCell* cell=[tableView
cellForRowAtIndexPath:indexPath];
//找到indexPath所对应的cell
cell.imageView.image=[UIImage imageNamed:@"logo.png"];
//cell中加入图像
initWithFrame:CGRectMake(0.0f, 0.0f, 320.f, 44.0f)];
label.backgroundColor=[UIColor redColor];
[cell.contentView addSubview:label];
[cell.contentView addSubview:new]; [cell viewWithTag:100];
//取得cell上tag值为100的对象,可以是任何具有tag值的对象
UINavigationController相关-----------------------
initWithRootViewControll
//创建一个当好控制器
self.window.rootViewController=navController;
//将一个对象加载到window
[self.navigationController.navigationBar addSubview:infoButton];//将按钮加到导航条
self.title=@"关于QQ";
//设置导航栏标题,等同于self.navigationContronller.title=@"关于QQ";
[self.navigationController
//即点击这一行的cell将进入chat界面,即将chat界面加入到导航中控制器中
[self.navigationController pushViewController:mvc
//运行
//将mvc放在栈的最顶上 controller切换用pushViewCOntroller
self.navigationItem.titleView=segmetControl;//UISegmentedControl *segmetControl;//将segmetControl加到导航条上
UIBarbuttonItem------------------
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:@"编辑"
style:UIBarButtonItemStylePlai
target:self
action:@selector(beginEdit:)];
//UIButtonBar 初始化 style:UIBarButtonItemStylePlai
self.navigationItem.rightBarButtonItem=rightButton;//把UIBarButton添加到导航指定位置
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]i
nitWithBarButtonSystemIt
target:self
action:@selector(addBookMark:)];
//用系统自带的图形初始化UIBarButton 如UIBarButtonSystemItemBoo
self.navigationItem.leftBarButtonItem=leftButton; //把UIBarButton添加到导航指定位置
UINavigationBar/UINavigationItem----------
UINavigationBar
[self.navigationController.navigationBar
动画相关-----------------------------
[UIView beginAnimations:@"animation" context:nil];//开始动画
[UIView setAnimationDuration:1];//动画持续时间
[UIView setAnimationCurve:UIViewAnimationCurveEase
[UIView
forView:[self.view superview]
cache:NO];
//setAnimationTransition动画具体切换方式
[UIView commitAnimations];//确认动画的执行
//qq项目图片箭头
CGContextRef
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.25f];
if (flag[section] == 0) {
img.transform = CGAffineTransformMakeRot
} else {
img.transform = CGAffineTransformMakeRot
}
[UIView commitAnimations];
[button addSubview:img];
[self performSelector:@selector(myDeselect:) withObject:nil afterDelay:0.5f];//在0.5秒后执行myDeselect方法
键盘------------------
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotific
//注册键盘显示事件
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotific
//注册键盘隐藏事件
// NSNotificationCenter 内部通知
-(void)keyboardShown:(NSNotification *)aNotification
NSDictionary *info=[aNotification userInfo];
NSValue *aValue=[info objectForKey:UIKeyboardFrameEndUserIn
CGSize keyboardSize=[aValue CGRectValue].size;
//得到键盘的大小
NSPredicate相关-----------------
NSPredicate *predicate=[NSPredicate
predicateWithFormat:@"SELF contains [cd] %",self.searchBar.text];
//self.searchBar.text取得搜索框的结果,为要找的数据
//[cd]中c不区分大小写,d不区分重音
NSArray *resule=[self.dataArray filteredArrayUsingPredic
//用谓词来过滤dataArray中的元素,返回一个过滤队列
//在这里搜索,把结果存在resultArray中