2

我希望这是一个非常简单的问题,但是在以下代码(没有弧和 *.nibs 的项目)之后我如何适应 UIButton 的框架:

    _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);

}

这是我的代码的结果: 在此处输入图像描述

那么,如何在没有不必要的填充的情况下将框架(绿色区域)适合内容(标题+图像)。我无法硬编码按钮框架的值,因为应用程序使用本地化。谢谢!

4

1 回答 1

0

我认为这不是一个好的解决方案,但我会发布它。任何意见将不胜感激))

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)];

所以,我刚刚手动设置了按钮大小,这就是现在的样子 剪裁

谢谢

于 2013-11-28T10:28:09.653 回答