IOS 常用控件方法大全

时间:2022-06-10 15:01:44

一.常用控件方法

1.imageview的图片点击事件

    UIImageView* imageview1 = [[UIImageViewalloc]initWithFrame:CGRectMake(10,10,300,50)];

    imageview1.image = [UIImageimageNamed:@"05.png"];

    imageview1.userInteractionEnabled =YES;

    UIGestureRecognizer *singleTap = [[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(UesrClicked)]autorelease];

    [imageview1 addGestureRecognizer:singleTap];

    [self.view addSubview:imageview1];  

   -(void)UesrClicked {

    NSLog(@"imageview is clicked!!");

   }

2.button

   UIButton* logout = [[UIButtonalloc]initWithFrame:CGRectMake(40,260,240,35)];    

    //UIButton 是继承自 UIControl,具有UIControlUIView的所有属性。

    logout.backgroundColor = [UIColorgreenColor];

    [logout setTitle:@"退出登陆" forState:UIControlStateNormal];               //设置正常状态下 Button标题

    [logout setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];//设置正常状态

    [logout addTarget:selfaction:@selector(onTextFieldButtonClicked:)forControlEvents:UIControlEventTouchUpInside];

   button上的title对齐

   Button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//左对齐(UIControlContentHorizontalAlignment、CenterUIControlContentHorizontalAlignmentFill、UIControlContentHorizontalAlignmentRight)
Button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;//底部对其(UIControlContentVerticalAlignmentCenter、UIControlContentVerticalAlignmentFill、UIControlContentVerticalAlignmentTop)


    [self.view addSubview:logout];


 

button添加图片的点击事件

     distanceButton = [[UIButtonalloc]init];

     distanceButton.frame =CGRectMake(220,10,80,40);

     [distanceButtonsetBackgroundImage:[UIImageimageNamed:@"item_default_bg.9.png"]forState:UIControlStateNormal];  //给button设置背景图片

     distanceButton.layer.cornerRadius =4.0;

     [distanceButtonaddTarget:selfaction:@selector(BtnClick:)forControlEvents:UIControlEventTouchUpInside];

    [self.contentViewaddSubview:distanceButton];

    [distanceButton release];

3.label   

    label2= [[UILabelalloc]initWithFrame:CGRectMake(15,50,250,30)];

    label2.font = [UIFontsystemFontOfSize:14.0];

    label2.text =@"成为传热维尔体育去维尔体育企鹅我让他有伍尔特";

    label2.textColor = [UIColorblackColor] ;

    label2.numberOfLines =0; //自动换行

    label2.adjustsFontSizeToFitWidth =YES;  //字体自适应

    label2.backgroundColor = [UIColorclearColor];

    [self.contentViewaddSubview:label2];

    [label2 release];


复杂颜色字体混合时,下面得字体测距方法比较好,虽然已经被舍弃,但是还是可以用的。  

FLT_MAX  :不考虑换行的话,写FLT_MAX,否知写死长度值(配合换行用,否则不换行了)。

FLT_MAX  :高度无限制,但是以实际高度为准


   productTextLabel = [[UILabel alloc] init];

        productTextLabel.text = [NSString stringWithFormat:@"                                  %@",[HsOTCDataModel getInstance].productName];

        productTextLabel.font = [UIFont systemFontOfSize:13.0f];

        productTextLabel.numberOfLines = 0;

        labelSize = [productTextLabel.text sizeWithFont:productTextLabel.font constrainedToSize:CGSizeMake(MAIN_SCREEN_WIDTH - 30, FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

        productTextLabel.frame  = CGRectMake(5, 10, labelSize.width, labelSize.height);

        productTextLabel.textColor = [HsConfigration uiColorFromString:@"#d06424"];

        productTextLabel.backgroundColor = [UIColor clearColor];

        [borderView addSubview:productTextLabel];


4.UITextField

   passwordtextField = [[UITextFieldalloc]initWithFrame:CGRectMake(10,60,MAIN_SCREEN_WIDTH,40)];

    //  textField.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:0 alpha:0.5];

    passwordtextField.borderStyle =UITextBorderStyleRoundedRect;                       //设置边框类型

    passwordtextField.secureTextEntry=YES;                                                //密码以小点代替

    //inputText.background = [UIImage imageNamed:@""];                           //设置背景图片

    passwordtextField.delegate =self;                                                  //设置委托对象

    passwordtextField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;//文字内容垂直居中,此为UIControl的属性

    [self.viewaddSubview:passwordtextField];

    passwordtextField.placeholder =@"请输入密码";   

5.对话框

 UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"登陆成功" message:nil delegate:self cancelButtonTitle:@"OK,I know" otherButtonTitles:nil];

         [alertView show];

         // [alertView release];

6.画线

UIView *divideLine = [[UIViewalloc]initWithFrame:CGRectMake(leftWidth,layoutHight+55,MAIN_SCREEN_WIDTH-2*leftWidth,1)];

        divideLine.backgroundColor = [RtStyleuiColorFromString:@"#e3e3e3"];//[UIColor redColor];

        [contentView addSubview: divideLine];

        [divideLine release];


7.边框

 //边框---------

        UIView *borderView2 = [[UIViewallocinitWithFrame:CGRectMake(leftWidth,layoutHight+180,MAIN_SCREEN_WIDTH - leftWidth*2,50)];

        borderView2.layer.backgroundColor = [[UIColorwhiteColorCGColor];

        borderView2.layer.cornerRadius =4.0f;

        borderView2.layer.borderWidth =1.0f;

       // borderView2.layer.borderColor = [[RtStyle uiColorFromString:@"#CAB194"] CGColor];

        [contentView addSubview:borderView2];

        [borderView2 release];


8.UIScrollView

UIScrollView *contentView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,2,MAIN_SCREEN_WIDTH,MAIN_SCREEN_HEIGHT-MAIN_TOPNAV_HEIGHT-3)];

        contentView.bounces = YES;

        contentView.contentSize = CGSizeMake( MAIN_SCREEN_WIDTH, 650);

        // contentView.backgroundColor = [UIColor redColor];

        [self addSubview:contentView];


二。常用方法总结

1. Button 处理函数

#pragma mark -------------------

#pragma mark textFieldButton 的响应函数

- (void)onTextFieldButtonClicked:(UIButton*)button{ 

    [accounttextFieldresignFirstResponder];

    [passwordtextFieldresignFirstResponder];  

    if(accounttextField.text.length==0||passwordtextField.text.length==0)

     {

   UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:@"用户名或密码不能为空" message:@"请键入用户名或密码" delegate:self cancelButtonTitle:@"OK,I know"    otherButtonTitles:nil];

   [alertView show];

        NSLog(@"用户名或密码不能为空");

     }

 elseif ([accounttextField.textisEqualToString:@"sww"]&&[passwordtextField.textisEqualToString:@"123456"])

    {

        /* UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"登陆成功" message:nil delegate:self cancelButtonTitle:@"OK,I know" otherButtonTitles:nil];

         [alertView show];*/

         // [alertView release];

        NSLog(@"登陆成功" );

        //这里使用展示方式跳转到LLViewController

        SettingViewController *settingViewControlle = [[SettingViewControlleralloc]init];

        [self presentViewController:settingViewControlleanimated:YEScompletion:^{          }];

}    

    else

    {

        NSLog(@"用户名或者密码错误!");

        UIAlertView *alertView=[[UIAlertViewalloc]initWithTitle:@"用户名或密码错误" message:@"请重新键入用户名或密码" delegate:self cancelButtonTitle:@"OK,I know"otherButtonTitles:nil];

[alertView show];

    }

}




三。字符串转换    

数字保留3个小数点:.3f亿元转换

 if([data.title isEqualToString:@"市值"]){

                if([[packet getMarketValue]intValue] >= 10000000){

                    data.numStr = [NSString stringWithFormat:@"%.3f亿",[[packet getBalance]intValue]/100000000.0];

                    // data.numStr = [[packet getBalance]intValue]/10000000.0;

                }

NSString *tempA = @"123"; 

NSString *tempB = @"456";  

1,字符串拼接 

  1. NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB];  

2,字符转int 

  1. int intString = [newString intValue];  

3,int转字符 

NSString *stringInt = [NSString stringWithFormat:@"%d",intString];  

4,字符转float 

float floatString = [newString floatValue];  

5,float转字符 

NSString *stringFloat = [NSString stringWithFormat:@"%f",intString];