Question

I hope, that it's quite simple question, but how I can fit UIButton's frame after following code (project without arc and *.nibs):

    _fotosButton = [UIButton buttonWithType: UIButtonTypeCustom];
    [_fotosButton setImage: [UIImage imageNamed: @"kino_gallery_normal.png"] forState: UIControlStateNormal];
    [_fotosButton setImage: [UIImage imageNamed: @"kino_gallery_highlight.png"] forState: UIControlStateHighlighted];
    [_fotosButton setTitle: NSLocalizedString(@"FOTOS", nil) forState: UIControlStateNormal];
    [_fotosButton setTitleColor: RGBCOLOR(11.0f, 176.0f, 225.0f) forState: UIControlStateNormal];
    [_fotosButton setTitleColor: [UIColor grayColor] forState: UIControlStateDisabled];
    [_fotosButton.titleLabel setFont:TTSTYLEVAR(kinoFont10)];
    [_fotosButton sizeToFit];

    [self updateButtonInsets:_fotosButton];
    [self addSubview: _fotosButton];

- (void) updateButtonInsets:(UIButton*) button {

   CGSize imageSize = button.imageView.frame.size;
   CGSize titleSize = button.titleLabel.frame.size;

   button.titleEdgeInsets = UIEdgeInsetsMake(0.0f, -imageSize.width, -imageSize.height, 0.0);

   titleSize = button.titleLabel.frame.size;
   button.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height, 0.0f, 0.0f, -titleSize.width);

}

This is a result of my code: enter image description here

So, how can I fit frame (green area) to the content(title+image) without unnecessary paddings. I can't hardcode value of button's frame, because app use Localisation. Thank you!

Was it helpful?

Solution

I don't think that's a good a solution, but I'll post it. Any comments will be appreciated))

titleSize = button.titleLabel.frame.size;
button.imageEdgeInsets = UIEdgeInsetsMake(-titleSize.height, 0.0f, 0.0f, -titleSize.width);

[button setSize:CGSizeMake(titleSize.width, titleSize.height + imageSize.height)];

So, I've just set button size manually and that's how it looks now clipped

Thank you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top