苹果开发 笔记(59)UIButton(2)

苹果开发 笔记(59)UIButton(1)

利用UIButton可以创建圆角和方形的按钮,这些按钮比较简单实用,经常可以在购买网站看到。 如下图效果

这里写图片描述

CGRect frame = CGRectMake(300, 100, 40, 40);
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    addBtn.frame = frame;
    [addBtn setTitle:@"+" forState: UIControlStateNormal];
    [addBtn setTitle:@"+" forState: UIControlStateHighlighted];
    [addBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    addBtn .titleLabel.font  = [UIFont systemFontOfSize:18];
    addBtn.layer.cornerRadius = addBtn.frame.size.width/2;
    addBtn.layer.borderColor  = [UIColor redColor].CGColor;
    addBtn.layer.borderWidth = 1;
    addBtn.center = CGPointMake(self.view.center.x+50,self.view.center.y);
    [self.view addSubview:addBtn];



    UIButton *subBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    subBtn.frame = frame;
    [subBtn setTitle:@"-" forState: UIControlStateNormal];
    [subBtn setTitle:@"-" forState: UIControlStateHighlighted];
    [subBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [subBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    subBtn .titleLabel.font  = [UIFont systemFontOfSize:18];
    subBtn.layer.cornerRadius = subBtn.frame.size.width/2;
    subBtn.layer.borderColor  = [UIColor redColor].CGColor;
    subBtn.layer.borderWidth = 1;
    subBtn.center = CGPointMake(self.view.center.x-50,self.view.center.y);
    [self.view addSubview:subBtn];


    UIButton *addRBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    addRBtn.frame = frame;
    [addRBtn setTitle:@"+" forState: UIControlStateNormal];
    [addRBtn setTitle:@"+" forState: UIControlStateHighlighted];
    [addRBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [addRBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    addRBtn .titleLabel.font  = [UIFont systemFontOfSize:18];
    addRBtn.layer.cornerRadius = 4;
    addRBtn.layer.borderColor  = [UIColor redColor].CGColor;
    addRBtn.layer.borderWidth = 1;
    addRBtn.center = CGPointMake(self.view.center.x+50,self.view.center.y+50);
    [self.view addSubview:addRBtn];



    UIButton *subRBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    subRBtn.frame = frame;
    [subRBtn setTitle:@"-" forState: UIControlStateNormal];
    [subRBtn setTitle:@"-" forState: UIControlStateHighlighted];
    [subRBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [subRBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    subRBtn .titleLabel.font  = [UIFont systemFontOfSize:18];
    subRBtn.layer.cornerRadius = 4;
    subRBtn.layer.borderColor  = [UIColor redColor].CGColor;
    subRBtn.layer.borderWidth = 1;
    subRBtn.center = CGPointMake(self.view.center.x-50,self.view.center.y+50);
    [self.view addSubview:subRBtn];

编译后 可以看到效果
这里写图片描述

做圆角还是圆形取决于需求,而这两个区别仅仅是对Layer 属性的layer.cornerRadius 设置而已。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值