iOS: UIButton resize according to text length

后端 未结 14 860
你的背包
你的背包 2020-11-29 17:28

In interface builder, holding Command + = will resize a button to fit its text. I was wondering if this was possible to do programmatically before the

14条回答
  •  萌比男神i
    2020-11-29 18:22

    sizeToFit doesn't work correctly. instead:

    myButton.size = myButton.sizeThatFits(CGSize.zero)
    

    you also can add contentInset to the button:

    myButton.contentEdgeInsets = UIEdgeInsetsMake(8, 8, 4, 8)

提交回复
热议问题