读书人

ios 开发的一些控件综合属性小结

发布时间: 2013-10-15 16:47:37 作者: rapoo

ios 开发的一些控件综合属性总结

坐标相关---------------------------

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;

CGFloatoneLineHeigth=[@"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=123

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];

// UIButton的默认类型为自定义类型 UIButtonTypeCustom,此类型UIBUTTON没有图示,看不见,但是能感应事件常用来感应响应事件。因为默认是不可见,所以手动写的时候,一定要修改其类型。


[buttonsetTitle:@"Pre" forState:UIControlStateNormal];

//UIButton设置标题不是通过label.text而是用setTitle方法。很容易出错


button.alpha=0.5f;//设置透明

[button setBackgroundImage:[UIImageimageNamed:@"title.png"]

forState:UIControlStateNormal];//给buttton设置背景图片


[button

addTarget:self action:@selector(headerClick:)

forControlEvents:UIControlEventTouchUpInside];

//给控件手动增加响应动作






UITabBarItem----------------------

//UITabBarItem是导航视图下面的对多五个,的小方块


UITabBarItem *tabBar=[[UITabBarItem alloc]

initWithTitle:@"机票查询"

image:[UIImage imageNamed:@"near.png"]

tag:4];


//初始化UITabBarItem

UITabBarItem *tabBar5=[[UITabBarItem alloc]

initWithTabBarSystemItem:UITabBarSystemItemMoretag:5

tag:3];

//用系统给定的方式初始化initWithTabBarSystemItem:UITabBarSystemItemMore



UITabBarController -----------------------------

//导航下部就是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 green:222/255 blue:333/255 alpha:1.0f];

//RGB颜色

UIColor *color=[UIColor clearColor]//透明色



读取文件------------------------------

//读取文本文件

[[NSBundle mainBundle] bundlePath]

NSString* path=[[NSBundle mainBundle]

pathForResource:@"1" ofType:@"txt"];

NSError* error;

NSString* contenString=[NSString

stringWithContentsOfFile:path

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];

//读取文件总结,是先用mainBundle获取路径,然后用xxxWithContentsOfFile



代理相关------------------------

<UITableViewDelegate, UITableViewDataSource>

<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 *)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常见属性

UITextField * textFieldRounded = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];

  • textFieldRounded.borderStyle = UITextBorderStyleRoundedRect;
  • textFieldRounded.textColor = [UIColor blackColor]; //text color
  • textFieldRounded.font = [UIFont systemFontOfSize:17.0];
  • textFieldRounded.placeholder = @"<enter text>"; //place holder
  • textFieldRounded.backgroundColor = [UIColor whiteColor];
  • textFieldRounded.autocorrectionType=UITextAutocorrectionTypeNo
  • textFieldRounded.keyboardType = UIKeyboardTypeDefault;
  • textFieldRounded.returnKeyType = UIReturnKeyDone;
  • textFieldRounded.clearButtonMode=UITextFieldViewModeWhileEditing;
  • 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)numberOfComponentsInPickerView:(UIPickerView *)pickerView

    //确定有多少列


    -(NSInteger)pickerView:(UIPickerView *)pickerView

    numberOfRowsInComponent:(NSInteger)component

    //每一列即每一个component有多少行


    -(CGFloat)pickerView:(UIPickerView *)pickerView

    widthForComponent:(NSInteger)component

    //返回每一列的宽度


    -(CGFloat)pickerView:(UIPickerView *)pickerView

    rowHeightForComponent:(NSInteger)component

    //设置每一行的高度


    -(UIView*)pickerView:(UIPickerView *)pickerView

    viewForRow:(NSInteger)row

    forComponent:(NSInteger)componentreusingView:(UIView *)view

    //返回具体某列某行的视图,在此函数中设置具体列中具体行



    -(void)pickerView:(UIPickerView *)pickerView

    didSelectRow:(NSInteger)row inComponent:(NSInteger)component

    //当选众某单元格时调用的函数


    int row0=[pickerView selectedRowInComponent:0];

    //获取在哪个component中选中的哪行


    UIView* view0=[pickerView viewForRow:row0 forComponent:0];

    //在component中的row中的view


    UILabel* label0=[view0 viewWithTag:200];

    //找出view中tag值为200的label




    UIDatePicker相关--------------------

    UIDatePicker* datePicker;

    [self.datePicker

    addTarget:self

    action:@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和当前的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 stretchableImageWithLeftCapWidth:21

    topCapHeight:14]];

    //img stretchableImageWithLeftCapWidth:21 topCapHeight:14为设置四角的宽和高,图像四角不变开始拉伸

    imgView.backgroundColor=[UIColor clearColor];



    cell设置相关--------------------------


    cell.tag

    cell.textLabel.text=str;


    cell.textLabel.textAlignment = UITextAlignmentRight

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    //设置cell访问的类型显示向右的箭头//accessory--附件Indicator--指示


    cell.selectionStyle=UITableViewCellSelectionStyleBlue;//cell的颜色

    cell.selectionStyle=UITableViewCellSelectionStyleGray

    UITableViewCell* cell=[tableView

    cellForRowAtIndexPath:indexPath];

    //找到indexPath所对应的cell


    cell.imageView.image=[UIImage imageNamed:@"logo.png"];

    //cell中加入图像




    修改cell背景的方法一

    UILabel* label=[[UILabelalloc]

    initWithFrame:CGRectMake(0.0f,0.0f,320.f, 44.0f)];

    label.backgroundColor=[UIColorredColor];

    [cell.contentViewaddSubview: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切换用pushViewCOntroller

    self.navigationItem.titleView=segmetControl;//UISegmentedControl *segmetControl;//将segmetControl加到导航条上




    UIBarbuttonItem------------------

    //该控件是导航上面左右侧的按钮,是一种按钮行控件不是view行控件

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]

    initWithTitle:@"编辑"

    style:UIBarButtonItemStylePlain

    target:self

    action:@selector(beginEdit:)];

    //UIButtonBar初始化 style:UIBarButtonItemStylePlain按钮的类型 target:self 添加位置action:@selector(beginEdit:)按下按钮后触发的事件


    self.navigationItem.rightBarButtonItem=rightButton;//把UIBarButton添加到导航指定位置

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]i

    nitWithBarButtonSystemItem: UIBarButtonSystemItemBookmarks

    target:self

    action:@selector(addBookMark:)];

    //用系统自带的图形初始化UIBarButton如UIBarButtonSystemItemBookmarks


    self.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:UIViewAnimationTransitionFlipFromLeft

    forView:[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:self

    selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil];

    //注册键盘显示事件


    [[NSNotificationCenter defaultCenter]

    addObserver:self

    selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotification object:nil];

    //注册键盘隐藏事件




    // NSNotificationCenter内部通知 self事件的观察者


    -(void)keyboardShown:(NSNotification *)aNotification

    NSDictionary *info=[aNotification userInfo];

    NSValue *aValue=[info objectForKey:UIKeyboardFrameEndUserInfoKey];

    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 filteredArrayUsingPredicate:predicate];

    //用谓词来过滤dataArray中的元素,返回一个过滤队列

    //在这里搜索,把结果存在resultArray中



  • 读书人网 >操作系统

    热点推荐