给button添加边框和圆角

本文详细介绍了如何使用UIButton控件创建具有丰富样式的按钮,并通过代码展示了如何设置按钮的标题、图片、背景颜色等属性,同时实现了按钮点击后的动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

button是我们经常用到的控件,我把它的属性罗列一下:

UIButton *Button = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 120, 50)];
    NSString *str = @"已认证";
    [Button setTitle:[NSString stringWithFormat:@"%@",str] forState:UIControlStateNormal];
    [Button setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
    [Button setImage:[UIImage imageNamed:@"i_ico_class.png"] forState:UIControlStateNormal];
    /* 这样的可以控制边缘插入的位置 可以 让一个按钮的背景里面既有图片也有文字,图片和文字的位置可以随意用此调整 */
//    [Button setImageEdgeInsets:str.length>1?UIEdgeInsetsMake(0, 3, 0, 0):UIEdgeInsetsMake(0, 20, 0, -40)];//上、左、下、右
    [Button setImageEdgeInsets:UIEdgeInsetsMake(0, 3, 0, 0)];//上、左、下、右
    
    Button.backgroundColor = RGB(87, 205, 66);
    Button.clipsToBounds = YES;//裁剪
    Button.titleLabel.font = [UIFont systemFontOfSize:18];//字体大小
    Button.layer.cornerRadius = 25;//圆角半径
    Button.layer.shadowOffset =  CGSizeMake(3, 5);
    Button.layer.shadowOpacity = 0.8;//阴影的透明度
    Button.layer.shadowColor =  RGB(87, 205, 66).CGColor;//阴影的颜色
    Button.layer.borderColor = RGBA(255, 255, 255, 0.4).CGColor;//边框的颜色
    Button.layer.borderWidth = 4;//边框的宽度
    [Button addTarget:self action:@selector(weekChooseAction:) forControlEvents:UIControlEventTouchUpInside];//绑定方法
    [self.view addSubview:Button];

//实现方法

  -(void)weekChooseAction:(id)sender
  {
      [UIView animateWithDuration:2 animations:^{
          self.view.backgroundColor = [UIColor redColor];
      }];
  }

 

转载于:https://www.cnblogs.com/hero11223/p/5175725.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值