UIButton

来源:互联网 发布:1元云购源码视频 编辑:程序博客网 时间:2024/06/11 10:56

/***********************************************************************************/

#import "AppDelegate.h"


@interface AppDelegate ()


@end


@implementation AppDelegate


-(void)dealloc

{

    [_window release];

    [super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

    self.window =[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    self.window.backgroundColor =[UIColorgrayColor];

    [self.windowmakeKeyAndVisible];

    [_window release];

    UIViewController *vc =[[UIViewControlleralloc] init];

    self.window.rootViewController=vc;

    [vc release];

    

    

//    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(50, 50,100, 50)];

//    label.backgroundColor =[UIColor yellowColor];

//    [self.window addSubview:label];

//    [label release];

//    label.text =@"程锦乐";    //添加汉字

//    label.textAlignment =1; //居中对齐

//    label.textColor =[UIColor blackColor];  //设置字体颜色

//    label.font=[UIFont systemFontOfSize:20]; //字体大小

//    label.shadowColor =[UIColor greenColor];  //阴影颜色

//    label.shadowOffset = CGSizeMake(5, 5);    //阴影大小

     /// 加边框

//

//    label.layer.borderWidth =1;

     ///加弧度

//    label.layer.cornerRadius =50;

    ///把多余的框(部分)隐藏掉

//    label.layer.masksToBounds =YES;


    /// UIButton

//    UIButton *button =[[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];

//    button.backgroundColor =[UIColor  orangeColor];

//    [self.window addSubview:button];

//    [button release];

//    /// UIButton提供了通过便利构造器创建的方法

//    UIButton *button =[UIButton buttonWithType:UIButtonTypeSystem];

//    ///设置尺寸

//    button.frame =CGRectMake(100, 100, 150, 50);

//    button.backgroundColor =[UIColor redColor];

//    [self.window addSubview:button];

//    ///通过这种方法创建不需要release

//     button.layer.borderWidth =1;     //边框

//    button.layer.cornerRadius = 50;   //弧度

//    /// button设置标题

//    [button setTitle:@"程锦乐" forState:UIControlStateNormal];

//    /// 标题颜色

//    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//    /// 标题大小

//    button.titleLabel.font =[UIFont systemFontOfSize:25];

//    ///  button 的点击方法

//    [button addTarget:self action:@selector(cilick:) forControlEvents:UIControlEventTouchUpInside ];

//    

    

//    UIButton *button1 =[UIButton buttonWithType:UIButtonTypeSystem];

//    button1.frame =CGRectMake(100,150 , 150, 50);

//    button1.backgroundColor =[UIColor  greenColor];

//    button1.layer.borderWidth =2;

//    button1.layer.cornerRadius = 48;

//    [self.window addSubview:button1];

//    [button1 setTitle:@"大地瓜" forState:UIControlStateNormal];

//    [button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//    button1.titleLabel.font =[UIFont systemFontOfSize:25];

//    [button1 addTarget:self action:@selector(cilick:) forControlEvents:UIControlEventTouchUpInside];

    

    

//    UIButton *button1 =[UIButton buttonWithType:UIButtonTypeSystem];

//    button1.frame =CGRectMake(100, 100, 150, 50);

//    button1.backgroundColor =[UIColor orangeColor];

//    button1.layer.borderWidth= 1;    //加边框

//    button1.layer.cornerRadius =25;  //加弧度

//    [self.window addSubview:button1];

    

    ///设置背景图片

       ///第一种方法

//    [button1 setBackgroundImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];

     ///第二种方法   UIButtonTypeSystem  换成   UIButtonTypeCustom

    UIButton *button2 =[UIButtonbuttonWithType:UIButtonTypeCustom];

    button2.frame=CGRectMake(100,150, 150, 50);

    [self.windowaddSubview:button2];

    button2.layer.borderWidth=1;   //加边框

    button2.layer.cornerRadius=25//加弧度

    [button2 setImage:[UIImageimageNamed:@"BtnOff.png"]forState:UIControlStateNormal];

    [button2 addTarget:selfaction:@selector(changeImage:)forControlEvents:UIControlEventTouchUpInside];

    

//    [button2 setTitle:@"你好" forState:UIControlStateNormal];

//    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    //custom 是自定义的button,system没什么太大区别,但是在设置图片上有区别,一个是setBackGroudImgaesetImage有区别,在一个就是custom设置标题还需要额外设置标题颜色

    

    

    

    

    

    

    

    return YES;

}


-(void)cilick:(UIButton *)button

//这个参数button就是触发方法对应的那个对象

//    NSLog(@"测试一下");

//    NSLog(@"%@",button.currentTitle);

   //点击button更换标题

    if ([button.currentTitleisEqualToString:@"程锦乐"])

    {

        [button setTitle:@"笑哈哈"forState:UIControlStateNormal];

    }else

    if ([button.currentTitleisEqualToString:@"笑哈哈"])

    {

        [button setTitle:@"程锦乐"forState:UIControlStateNormal];

    }

     

    

}


-(void)changeImage:(UIButton *)button

{

    NSLog(@"%d",button.selected);

    if (button.selected ==NO)

    {

        [button setImage:[UIImageimageNamed:@"BtnOn.png"]forState:UIControlStateNormal];

    }else

        {

            [button setImage:[UIImageimageNamed:@"BtnOff.png"]forState:UIControlStateNormal];

        }

    button.selected = !button.selected;

}


0 0
原创粉丝点击