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

后端 未结 4 1033
一生所求
一生所求 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:42

    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;
    }
    
    0 讨论(0)
  • 2021-02-06 15:46

    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];
    
    }
    
    0 讨论(0)
  • 2021-02-06 15:50
    [btn sizeToFit]
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题