【问题标题】:UIButton titleLabel make as a circleUIButton titleLabel 做成一个圆圈
【发布时间】:2017-12-06 10:00:53
【问题描述】:

您好,我想将UIButton 标题标签设为一个圆圈。所以我就这样做了。

btnAttachments.setTitle(strcount, for: .normal)
btnAttachments.setTitleColor(UIColor.white, for: .normal)
btnAttachments.titleLabel?.backgroundColor=com.getRedcolor()
btnAttachments.titleLabel?.font=UIFont.init(name: "Bariol", size: 15)
btnAttachments.titleLabel?.layer.cornerRadius=(btnAttachments.titleLabel?.frame.width)!/2

但它并没有使它成为一个圆圈。看起来这段代码对titleLabel没有影响

更新

这就是我想做的

请帮帮我 谢谢

【问题讨论】:

  • 我想你忘记了 btnAttachments.titleLabel.clipsToBounds = YES
  • 我做了,还是一样:(为什么会这样?
  • 我认为 btnAttachments.titleLabel?.frame.width = 0。我尝试 btnAttachments.titleLabel?.layer.cornerRadius = 10。它正在工作
  • 你想要一个圆形的 UIButton?
  • 试试下面的答案,你需要将masksToBounds设置为true

标签: ios swift3 uibutton uilabel


【解决方案1】:

首先您必须验证 uibutton 的 clipsToBounds 是否为真,但如果已经设置并且问题仍然存在,那么您可能会尝试在 viewDidLoad 中实现这一点,它“应该”工作,但出于某种原因viewDidLoad 布局中的某些 xcode 8 版本仍然不是最终版本(对象尚未设置其真实尺寸),如果您发现一些不连贯的情况,您可以在分配角半径之前通过记录 uibutton 框架来验证这一点问题,如果这是问题,那么您只需将代码移动到 viewWillAppear

【讨论】:

    【解决方案2】:

    您缺少设置 MasToBounds

    btnAttachments.titleLabel?.layer.masksToBounds = true;
    

    添加这个你的代码会正常工作,

    你应该在viewDidLayoutSubViewsdrawRect中设置corner redius(通常在UIView类中)

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      使用下面的代码得到想要的结果:

      let btnAttachments = UIButton(type: .custom)
      btnAttachments.setTitle(strcount, for: .normal).setTitleColor(UIColor.white, for: .normal)
      btnAttachments.frame = CGRect(x: 160, y: 100, width: 50, height: 50)
      btnAttachments.titleLabel?.backgroundColor=com.getRedcolor()
      btnAttachments.titleLabel?.font=UIFont.init(name: "Bariol", size: 15)
      btnAttachments.layer.cornerRadius = 0.5 * button.bounds.size.width
      btnAttachments.clipsToBounds = true
      

      【讨论】:

        猜你喜欢
        • 2016-10-12
        • 2020-06-06
        • 2015-04-15
        • 1970-01-01
        • 2018-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        相关资源
        最近更新 更多