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.
:)
精彩评论