Xcode UIView 中的Button 控件的属性和基本用法

  //第一种创建UIButton的方法

    //initWhitFrame

    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(110, 100, 100, 30)];

    button.backgroundColor = [UIColor redColor];

    button.titleLabel.font = [UIFont systemFontOfSize:19.0];//设置按钮的文字大小

    button.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;//设置按钮文字的位置(默认为居中)

    button.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);//字体靠左或右时与边缘的距离

    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];//设置按钮文字的颜色

    [button setTitle:@"LYD" forState:UIControlStateNormal];

    [self.window addSubview:button];

    [button release];

    //第二种创建按钮的方法

    //buttonWithType

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

    button1.layer.cornerRadius = 10.0;

    //UIButtonTypeInfoLight 微件(widget)使用的蓝色小圆圈信息按钮,可以放在任何文字旁边

    //UIButtonTypeRoundedRect 圆角矩形

    //UIButtonTypeInfoDark  白色背景下使用的深色圆圈信息按钮

    //UIButtonTypeContactAdd 圆圈中一个加号的信息按钮

    //UIButtonTypeDetailDisclosure 蓝色的披露按钮,可放在任何文字旁

    CGRect RECT = CGRectMake(110, 100, 30, 30);

    button1.frame = RECT;

    /*//button1.selected = YES;

    //button1.enabled = NO;

    //设置标题,状态正常

    [button1 setTitle:@"normal" forState:UIControlStateNormal];

    //设置标题,状态高亮

    [button1 setTitle:@"highlighted" forState:UIControlStateHighlighted];

    //设置标题,状态禁用

    [button1 setTitle:@"disabled" forState:UIControlStateDisabled];

    //设置标题,状态选中

    [button1 setTitle:@"selected" forState:UIControlStateSelected];*/

    //设置背景图片

        [button1 setBackgroundImage:[UIImage imageNamed:@"button_white"] forState:UIControlStateNormal];

       [button1 setBackgroundImage:[UIImage imageNamed:@"button_red"] forState:UIControlStateSelected];

    //设置图片

//    [button1 setImage:[UIImage imageNamed:@"button_white"] forState:UIControlStateNormal];

//    [button1 setImage:[UIImage imageNamed:@"button_red"] forState:UIControlStateSelected];

    //添加事件

    //用户控件之内触摸抬按下时

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

    //当用户按下的时候触发

    //[button1 addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];

    //当用户在触摸控件后在控件之外拖拖动时

   //[button1 addTarget:self action:@selector(dragOutside) forControlEvents:UIControlEventTouchDragOutside];

 //     当用户在触摸控件后在控件之内拖拖动时

     //[button1 addTarget:self action:@selector(dragInside) forControlEvents:UIControlEventTouchDragInside];

    //用户控件之外触摸抬按下时

     //[button1 addTarget:self action:@selector(outside) forControlEvents:UIControlEventTouchUpOutside];

    //触摸控件往外拖时

      [button1 addTarget:self action:@selector(dragExit) forControlEvents:UIControlEventTouchDragExit];

    //触摸控件往内拖时

    [button1 addTarget:self action:@selector(dragEnter) forControlEvents:UIControlEventTouchDragEnter];

    //多次触摸控件时触发

    [button1 addTarget:self action:@selector(repeat) forControlEvents:UIControlEventTouchDownRepeat];

    button1.backgroundColor = [UIColor whiteColor];

    button1.titleLabel.font = [UIFont systemFontOfSize:18.0];

    [button1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    //button1.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

    //button1.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

    self.window.backgroundColor = [UIColor purpleColor];

    [self.window addSubview:button];

原文地址:https://www.cnblogs.com/meixian/p/4534348.html