UILabel UISwitch UISegmentedControl UIAlertView

基础小控件

    /***************************************UIlabel*************************************/

    UILabel *label = [[UILabel alloc]init];//创建UIlabel对象

    label.frame = CGRectMake(50, 50, 100, 50);//设定label的位置与大小

    label.backgroundColor = [UIColoryellowColor];//设定label的背景颜色

    label.text = @"123";//label添加文本

    label.tag = 1;//设置label标签,便于以后使用

    label.textAlignment = NSTextAlignmentRight;//设置文本对齐方式,系统默认为字体居右

    label.textColor = [UIColorblackColor];//设置字体的颜色

    label.numberOfLines = 0;//设置文本的行数,0代表有无线行

    [label.layersetCornerRadius:10.0];//设置label边框角为圆角,后面数值为圆角的半径

    label.font = [UIFontsystemFontOfSize:15];//设置字体的大小

    label.adjustsFontSizeToFitWidth = YES;//设置字体大小根据label的大小自动调整

    [self.view addSubview:label];//显示label

    /***************************************UIlabel*************************************/

 

 

 

 

    /****************************************UISwitch***********************************/

    UISwitch *sw = [[UISwitchalloc]init];

    sw.frame = CGRectMake(150, 150, 100, 100);//设置位置和大小,发现大小无效。即开关控件大小不受影响

    sw.on = YES;//设置开关的状态(开/关)

    sw.onImage=[UIImageimageNamed:@"1.png"];//无效

    sw.offImage=[UIImageimageNamed:@"logo.png"];//无效,即UISwitch上不能添加图片

    [sw addTarget:selfaction:@selector(loop) forControlEvents:UIControlEventValueChanged];//绑定监听方法

    [self.view addSubview:sw];//显示sw

    /****************************************UISwitch***********************************/

 

 

 

    

    /***************************************UISegmentControl****************************/

    UISegmentedControl *seg = [[UISegmentedControlalloc]initWithItems:@[@"",@"",@""]];//初始化seg,并且给seg分段

    seg.frame = CGRectMake(50, 200, 200, 50);//设置seg的位置与大小

    seg.backgroundColor = [UIColorwhiteColor];//设置seg的背景颜色

    [seg setTitle:@"登录"forSegmentAtIndex:0];//给莫一段设置标题

    //[seg setImage:[UIImage imageNamed:@"lun"] forSegmentAtIndex:1];//必须添加背景为透明的图片

    [seg insertSegmentWithTitle:@"2"atIndex:1animated:YES];//在原有的基础上添加一段,可以设置该段所放的位置与标题

    unsigned long int  i = seg.numberOfSegments;//获得seg的总段数

    NSLog(@"段数%zi",i);

    NSString *s = [seg titleForSegmentAtIndex:2];//获得某一段的标题

    NSLog(@"标题%@",s);

    UIImage *image = [seg imageForSegmentAtIndex:1];//获得某一段的图片

    NSLog(@"图片%@",image);

    [seg setWidth:60forSegmentAtIndex:1];//设置某一段的宽度

    [seg setEnabled:NO];//激活seg

    [seg setEnabled:NOforSegmentAtIndex:2];//激活某一段,yes为激活

    seg.selectedSegmentIndex = 2;//设置默认选中项

    [seg setBackgroundImage:[UIImageimageNamed:@"lun"] forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault];

    [seg addTarget:selfaction:@selector(loop1:) forControlEvents:UIControlEventTouchUpInside];//seg的监听事件

    [self.view addSubview:seg];

 

    /***************************************UISegmentControl****************************/

 

 

 

 

 

    /***************************************UIAlertView********************************/

    UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"是否注册"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:@"取消", nil];

    [alert show];

 

    /***************************************UIAlertView********************************/

 

 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    NSLog(@"%ld",(long)buttonIndex);

 

 

}//UIAlertView的监听事件

原文地址:https://www.cnblogs.com/lcl15/p/4960138.html