开发者

Resize of UIButton Alters the type of UIButton

开发者 https://www.devze.com 2023-02-08 06:28 出处:网络
I am building an app in which I need to resize the button as per the title length. I wrote the following code

I am building an app in which I need to resize the button as per the title length. I wrote the following code

`UIButton *newButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

newButton.backgroundColor = [ UIColor clearColor];
newButton.titleLabel.backgroundColor = [ UIColor whiteColor];
[newButton setTitle:@"devsri" forState:UIControlStateNormal];
newButton.t开发者_JAVA百科itleLabel.textColor = [ UIColor blackColor];

CGSize expectedLabelSize = [newButton.titleLabel.text sizeWithFont:newButton.titleLabel.font]; 
newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width, expectedLabelSize.height);

The above code do resize the table but the button no more remains round rectangle in view. Kindly let me know what is wrong in the above code.

Thanks in advance!!


I got the bug in here. I was initializing the frame with the size of the titlelabel that was eventually bigger than the original button in width. Thus the line

newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width, expectedLabelSize.height);

as

newButton.frame = CGRectMake(xBase, yBase, expectedLabelSize.width + 15, expectedLabelSize.height);

This will widen the button enough to accomodate the new size of the label.

:)

0

精彩评论

暂无评论...
验证码 换一张
取 消