iOS开发 ----- UIButton

UIButton

//创建 button 也可以用alloc init方式创建 button的类型为自定义类型
    UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom];
//初始化两张图片
    UIImage * image = [UIImage imageNamed:@"1.png"];
    UIImage * bgImage = [UIImage imageNamed:@"map.png"];
//设置button的前景图
    [button2 setImage:image forState:UIControlStateNormal];
//设置button的背景图
    [button2 setBackgroundImage:bgImage forState:UIControlStateNormal];
//设置button的位置大小
    button2.frame = CGRectMake(0, 80, 375, 30);
//设置点击的时候是否可以高亮
    button2.showsTouchWhenHighlighted = YES;
//添加到视图上
    [self.view addSubview:button2];


//初始化button        
    UIButton * button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(0, 120, 375, 30);
//设置button的标题
    [button3 setTitle:@"按钮" forState:UIControlStateNormal];
//设置高亮时的标题
    [button3 setTitle:@"llalala" forState:UIControlStateHighlighted];
//设置背景颜色
    [button3 setBackgroundColor:[UIColor grayColor]];
//设置button的字体的颜色
    [button3 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
//设置button是否可用
    button3.enabled = YES;
//设置button的唯一标示符
    button3.tag = 101;
//button的点击方法 自己写onClick方法即可
    [button3 addTarget:self action:@selector(onClick:)forControlEvents:UIControlEventTouchUpInside];

//其实button上边的字体相当于一个label,可以利用label的相关属性进行设置

//设置圆角矩形
    button3.layer.cornerRadius = 10;
//设置边框的颜色
    button3.layer.borderColor = [UIColor grayColor].CGColor;
//设置边框的宽度
    button3.layer.borderWidth = 3;
//设置button上的字体的大小
    button3.titleLabel.font = [UIFont fontWithName:@"Menlo" size:30];
//设置字体的背景颜色
    button3.titleLabel.backgroundColor = [UIColor redColor];
//设置 图片的位置 上左下右
    button.imageEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 40);
//设置 内容的位置 上左下右
    button.contentEdgeInsets = UIEdgeInsetsMake(30, 10, 0, 0);
//设置 文字的位置 上左下右
    button.titleEdgeInsetsEdgeInsets = UIEdgeInsetsMake(30, 10, 0, 0);
//把button添加到view上
    [self.view addSubview:button3];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值