我希望这是一个非常简单的问题,但是在以下代码(没有弧和 *.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);
}
这是我的代码的结果:
那么,如何在没有不必要的填充的情况下将框架(绿色区域)适合内容(标题+图像)。我无法硬编码按钮框架的值,因为应用程序使用本地化。谢谢!