How can i increase the button width dynamically depends on the text size in iphone?

后端 未结 4 1086
一生所求
一生所求 2021-02-06 15:12

I have created 10 buttons programmatically and set the titles in the button. Now i want to increase the button frame size dynamically,its depends on the text.

I given so

4条回答
  •  遇见更好的自我
    2021-02-06 15:59

    Very simple solution:

    TestButton.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 8);
    [TestButton sizeToFit];
    float width  =   TestButton.frame.size.width + 20;
    TestButton.frame = CGRectMake(TestButton.frame.origin.x, TestButton.frame.origin.y, width, 40);
    

提交回复
热议问题