各种控件属性总结

  • 格式:docx
  • 大小:153.07 KB
  • 文档页数:12

控件综合属性总结坐标相关---------------------------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 realsize = [str sizeWithFont:font constrainedToSize:CGSizeMake(250.0f, 1000.0f)lineBreakMode:UILineBreakModeWordWrap];//切页经典函数,根据给定的字体和大小范围,返回一个真实的大小UIFont相关-----------------------UIFont* font=[UIFont fontWithName:@"Arial" size:40.0f];//字体格式UIFont *font = [UIFont systemFontOfSize:13];NSArray* fontNArray=[UIFont familyNames];//familyNames返回系统所有的字体类型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.numberOfLines=3//限制行数有三行label.lineBreakMode=UILineBreakModeCharacterWrap;//设置折行方式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]; //设置view的背景色为透明色for(UIView *subview in [cell.contentView subviews])//获取view的子视图//UITabViewCell中的contentView包括textlabel和imageView[subview removeFromSuperview];//从父视图中移除子视图UIButton相关------------------------UIButton *button;button.frame=CGRectMake(280, 12, 20, 20);button.tag=123UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];// UIButton的默认类型为自定义类型UIButtonTypeCustom,此类型UIBUTTON没有图示,看不见,但是能感应事件常用来感应响应事件。

因为默认是不可见,所以手动写的时候,一定要修改其类型。

[button setTitle:@"Pre" forState:UIControlStateNormal];//UIButton设置标题不是通过label.text而是用setTitle方法。

很容易出错button.alpha=0.5f;//设置透明[buttonsetBackgroundImage:[UIImageimageNamed:@"title.png"]forState:UIControlStateNormal];//给buttton设置背景图片[buttonaddTarget:self action:@selector(headerClick:)forControlEvents:UIControlEventTouchUpInside];//给控件手动增加响应动作UITabBarItem----------------------//UITabBarItem是导航视图下面的对多五个,的小方块UITabBarItem *tabBar=[[UIT abBarItem alloc]initWithTitle:@"机票查询"image:[UIImage imageNamed:@"near.png"]tag:4];//初始化UITabBarItemUITabBarItem*tabBar5=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMoretag:5tag:3];//用系统给定的方式初始化initWithTabBarSystemItem:UITabBarSystemItemMoreUITabBarController -----------------------------//导航下部就是UITabBarControllerUITabBarController *tabBarControllertabBarController.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.textUIColor相关------------------------------UIColor *color=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg.jpg"]];//UIImage到UIColor的转换,用图像做背景的很重要步骤UIColor *color=[UIColorcolorWithRed:111/255 green:222/255 blue:333/255alpha:1.0f];//RGB颜色UIColor *color=[UIColor clearColor]//透明色读取文件------------------------------//读取文本文件[[NSBundle mainBundle] bundlePath]NSString* path=[[NSBundle mainBundle]pathForResource:@"1" ofType:@"txt"];NSError* error;NSString* contenString=[NSStringstringWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:&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];//读取文件总结,是先用mainBundle 获取路径,然后用xxxWithContentsOfFile代理相关------------------------<UITableViewDelegate, UIT ableViewDataSource><UIAlertViewDelegate><UITextFieldDelegate><UIPickerViewDelegate, UIPickerViewDataSource>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 *)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex {NSLog(@"你按下的按钮的index为%d", buttonIndex);//可以在此添加对话框触发的事件}UIAlertView* av=[[UIAlertView alloc]initWithTitle:@"信息"message:msgdelegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"确定", @"知道了",nil];//点击所弹出的对话框[av show];[av release];UITextField相关-------------------------手动设置UIT extField常见属性UITextField* ageT extField;-(BOOL)textFieldShouldReturn:(UITextField *)textField//点击return后执行此函数{[TextField resignFirstResponder];//让它失去第一响应者,从而点击此按键后键盘消失并执行函数中的代码NSLog(@"return key down");return YES;}-(IBAction)touchView:(id)sender{NSLog(@"touchView");[TextField resignFirstResponder];[self.ageTextField resignFirstResponder];}textField.text=@"";UIPickerView相关---------------------------(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView//确定有多少列-(NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component//每一列即每一个component有多少行-(CGFloat)pickerView:(UIPickerView *)pickerViewwidthForComponent:(NSInteger)component//返回每一列的宽度-(CGFloat)pickerView:(UIPickerView *)pickerViewrowHeightForComponent:(NSInteger)component//设置每一行的高度-(UIView*)pickerView:(UIPickerView *)pickerViewviewForRow:(NSInteger)rowforComponent:(NSInteger)componentreusingView:(UIView *)view//返回具体某列某行的视图,在此函数中设置具体列中具体行-(void)pickerView:(UIPickerView *)pickerViewdidSelectRow:(NSInteger)row inComponent:(NSInteger)component// 当选众某单元格时调用的函数int row0=[pickerView selectedRowInComponent:0];//获取在哪个component中选中的哪行UIView* view0=[pickerView viewForRow:row0 forComponent:0];//在component中的row中的viewUILabel* label0=[view0 viewWithTag:200];//找出view中tag值为200的labelUIDatePicker相关--------------------UIDatePicker* datePicker;[self.datePickeraddTarget:selfaction:@selector(dateChanged:)forControlEvents:UIControlEventValueChanged];//点击datepicker所触发事件if(self.datePicker.superview) //datePicker有父视图说明此视图才显示[self.datePicker removeFromSuperview];//将视图从其父视图移除NSDateFormatter* dateFormatter=[[NSDateFormatter alloc]init];//日期显示方式[dateFormatter setDateStyle:NSDateFormatterShortStyle];[dateFormatter setTimeStyle:NSDateFormatterShortStyle];dateLabel.text=[dateFormatter stringFromDate:self.datePicker.date];//在label上以NSDateFormatter显示UISlider相关------------------UISlider* slider=(UISlider*)sender;int val=[slider value];//取出UISlider的值UISearchDisplayController相关------------------UISearchDisplayController *searchDc;//显示搜索结果self.searchDc=[[[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self]autorelease];//初始化并关联searchBar和当前的UIViewControlUIImageView相关-------------------------------------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 stretchableImageWithLeftCapWidth:21topCapHeight:14]];//img stretchableImageWithLeftCapWidth:21 topCapHeight:14为设置四角的宽和高,图像四角不变开始拉伸imgView.backgroundColor=[UIColor clearColor];cell设置相关--------------------------cell.tagcell.textLabel.text=str;cell.textLabel.textAlignment = UITextAlignmentRightcell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;//设置cell访问的类型显示向右的箭头//accessory--附件Indicator--指示cell.selectionStyle=UITableViewCellSelectionStyleBlue;//cell的颜色cell.selectionStyle=UITableViewCellSelectionStyleGrayUITableViewCell* cell=[tableViewcellForRowAtIndexPath:indexPath];//找到indexPath所对应的cellcell.imageView.image=[UIImage imageNamed:@"logo.png"];//cell中加入图像修改cell背景的方法一UILabel* label=[[UILabel alloc]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相关-----------------------UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:rvc];//创建一个当好控制器self.window.rootViewController=navController;//将一个对象加载到window[self.navigationController.navigationBar addSubview:infoButton];//将按钮加到导航条self.title=@"关于QQ";//设置导航栏标题,等同于self.navigationContronller.title=@"关于QQ";[self.navigationController pushViewController:chat animated:YES];//即点击这一行的cell将进入chat界面,即将chat界面加入到导航中控制器中[self.navigationController pushViewController:mvc animated:YES];//运行pushViewController:mvc//将mvc放在栈的最顶上controller切换用pushViewCOntrollerself.navigationItem.titleView=segmetControl;//UISegmentedControl *segmetControl;//将segmetControl加到导航条上UIBarbuttonItem------------------//该控件是导航上面左右侧的按钮,是一种按钮行控件不是view行控件UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"编辑"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(beginEdit:)];//UIButtonBar 初始化style:UIBarButtonItemStylePlain 按钮的类型target:self 添加位置action:@selector(beginEdit:)按下按钮后触发的事件self.navigationItem.rightBarButtonItem=rightButton;//把UIBarButton添加到导航指定位置UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemBookmarkstarget:selfaction:@selector(addBookMark:)];//用系统自带的图形初始化UIBarButton 如UIBarButtonSystemItemBookmarksself.navigationItem.leftBarButtonItem=leftButton;//把UIBarButton添加到导航指定位置UINavigationBar/UINavigationItem----------UINavigationItem是继承自NSObject用来定制导航条上的项目UINavigationBar 是继承自NSVIew是用来定制导航的属性[self.navigationController.navigationBar addSubView:button];动画相关-----------------------------[UIView beginAnimations:@"animation" context:nil];//开始动画[UIView setAnimationDuration:1];//动画持续时间[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//动画切换方式[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeftforView:[self.view superview]cache:NO];//setAnimationTransition动画具体切换方式[UIView commitAnimations];//确认动画的执行//qq项目图片箭头CGContextRef context = UIGraphicsGetCurrentContext();[UIView beginAnimations:nil context:context];[UIView setAnimationDuration:0.25f];if (flag[section] == 0) {img.transform = CGAffineTransformMakeRotation(-3.14/2);} else {img.transform = CGAffineTransformMakeRotation(3.14/2);}[UIView commitAnimations];[button addSubview:img];[self performSelector:@selector(myDeselect:) withObject:nil afterDelay:0.5f];//在0.5秒后执行myDeselect方法键盘------------------//键盘隐藏显示[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardShown:)name:UIKeyboardDidShowNotification object:nil];//注册键盘显示事件[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardHidden:)name:UIKeyboardDidHideNotification object:nil];//注册键盘隐藏事件// NSNotificationCenter 内部通知self事件的观察者-(void)keyboardShown:(NSNotification *)aNotificationNSDictionary *info=[aNotification userInfo];NSValue *aValue=[info objectForKey:UIKeyboardFrameEndUserInfoKey];CGSize keyboardSize=[aValue CGRectValue].size;//得到键盘的大小NSPredicate相关-----------------NSPredicate *predicate=[NSPredicatepredicateWithFormat:@"SELF contains [cd] %",self.searchBar.text];//self.searchBar.text取得搜索框的结果,为要找的数据//[cd]中c不区分大小写,d不区分重音NSArray *resule=[self.dataArray filteredArrayUsingPredicate:predicate];//用谓词来过滤dataArray中的元素,返回一个过滤队列//在这里搜索,把结果存在resultArray中。