views:

125

answers:

3

Hi Buddies,

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 some conditions and set the frame size. but how can i set the exact frame size depends on the text(get the text dynamically).

Here my sample code is,

     float x=0, y=0, w, h=20;

    for(int i = 100; i < 110; i++)
     {
         btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

         UIImage * img = [UIImage imageNamed:@"Round_Rect4.png"];

        [btn setBackgroundImage:img forState:UIControlStateSelected];

        titleString = [titleArray objectAtIndex:i-100];  // get button title

        if([titleString length] <= 5)
        {
            w = 50;
            btn.frame = CGRectMake(x,y,w,h); 

            x = x + 70;
        }

        if (([titleString length] >= 6) && ([titleString length] <=10))
       {
             w = 70;

             btn.frame = CGRectMake(x,y,w,h); 

             x = x + 90;
       } 

       if(([titleString length] >= 11) && ([titleString length] <=15))
       {
             w = 105;
             btn.frame = CGRectMake(x,y,w,h); 

             x = x + 120;

       }

       if([titleString length] >= 16)
       {
             w = 120;
             btn.frame = CGRectMake(x,y,w,h); 

             x = x + 140;

       }

        [btn setTitle:titleString forState: UIControlStateNormal];

        [btn setTag:i];

        [self.view addSubview:btn];

}

see the example image,

image-2 image-1

So is this possible to set the exact button frame size which depends on the text?, plz guide me.

Thanks

+3  A: 
[btn sizeToFit]
tc.
Heehee. But that doesnt really take in account that the background image has rounded corners.
John Ballinger
No; you have to do that yourself — something like `btn.contentEdgeInsets = (UIEdgeInsets){.left=8,.right=8}`
tc.
+1  A: 

Copy an pasted from Apples Bubble Level source code.

This is the line you want

UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];

full code here

- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image {
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
    [button setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown];
    UIImage *newImage = [image stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    [button addTarget:target action:inSelector forControlEvents:UIControlEventTouchUpInside];
    button.adjustsImageWhenDisabled = YES;
    button.adjustsImageWhenHighlighted = YES;
    [button setBackgroundColor:[UIColor clearColor]];   // in case the parent view draws with a custom color or gradient, use a transparent color
    [button autorelease];
    return button;
}
John Ballinger
A: 

Hi,

I got the answer and my working code is,

    float x=0;

    for(int i = 100; i < 110; i++)
     {
         btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

         UIImage * img = [UIImage imageNamed:@"Round_Rect4.png"];

        [btn setBackgroundImage:img forState:UIControlStateSelected];

        titleString = [titleArray objectAtIndex:i-100];  // get button title

       CGSize fontSize = [titleString sizeWithFont:[UIFont systemFontOfSize:12.0]];

        CGRect currentFrame = btn.frame;

        CGRect buttonFrame = CGRectMake(x, currentFrame.origin.y, fontSize.width + 22.0, fontSize.height + 12.0);

       [btn setFrame:buttonFrame];

        x = x + fontSize.width + 35.0;

        [btn setTitle:titleString forState: UIControlStateNormal];

        [btn setTag:i];

        [self.view addSubview:btn];

}

Pugal Devan
Why not `[titleString sizeWithFont:btn.font]`?
tc.
If i used [titleString sizeWithFont:btn.font] this code and the warning is displayed.(Font is deprecated).
Pugal Devan