Label under image in UIButton(UIButton中图像下的标签)
                            本文介绍了UIButton中图像下的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
                        
                        问题描述
我正在尝试创建一个在图标下方有一些文本的按钮(有点像应用程序按钮),但它似乎很难实现.有什么想法可以让我用 UIButton 在图像下方显示文本?
I'm trying to create a button which has some text beneath the icon (sorta like the app buttons) however it seems to be quite difficult to achieve. Any ideas how can I go about get the text to display below the image with a UIButton?
推荐答案
或者你可以只使用这个类别:
Or you can just use this category:
@interface UIButton (VerticalLayout)
- (void)centerVerticallyWithPadding:(float)padding;
- (void)centerVertically;
@end
@implementation UIButton (VerticalLayout)
- (void)centerVerticallyWithPadding:(float)padding {
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    CGFloat totalHeight = (imageSize.height + titleSize.height + padding);
    
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height),
                                            0.0f,
                                            0.0f,
                                            - titleSize.width);
    
    self.titleEdgeInsets = UIEdgeInsetsMake(0.0f,
                                            - imageSize.width,
                                            - (totalHeight - titleSize.height),
                                            0.0f);
    
    self.contentEdgeInsets = UIEdgeInsetsMake(0.0f,
                                              0.0f,
                                              titleSize.height,
                                              0.0f);
}
- (void)centerVertically {
    const CGFloat kDefaultPadding = 6.0f;
    [self centerVerticallyWithPadding:kDefaultPadding];
}
@end
Swift 扩展
extension UIButton {
    
    func centerVertically(padding: CGFloat = 6.0) {
        guard
            let imageViewSize = self.imageView?.frame.size,
            let titleLabelSize = self.titleLabel?.frame.size else {
            return
        }
        
        let totalHeight = imageViewSize.height + titleLabelSize.height + padding
        
        self.imageEdgeInsets = UIEdgeInsets(
            top: -(totalHeight - imageViewSize.height),
            left: 0.0,
            bottom: 0.0,
            right: -titleLabelSize.width
        )
        
        self.titleEdgeInsets = UIEdgeInsets(
            top: 0.0,
            left: -imageViewSize.width,
            bottom: -(totalHeight - titleLabelSize.height),
            right: 0.0
        )
        
        self.contentEdgeInsets = UIEdgeInsets(
            top: 0.0,
            left: 0.0,
            bottom: titleLabelSize.height,
            right: 0.0
        )
    }
    
}
建议:如果按钮高度小于totalHeight,则图像将绘制到外边框.
Suggestion:
If button height is less than totalHeight, then image will draw outside borders.
imageEdgeInset.top 应该是:
max(0, -(totalHeight - imageViewSize.height))
                        这篇关于UIButton中图像下的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
				 沃梦达教程
				
			本文标题为:UIButton中图像下的标签
				
        
 
            
        
             猜你喜欢
        
	     - 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
 - 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
 - Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
 - GPS状态的广播接收器? 2022-01-01
 - UITextView 内容插图 2022-01-01
 - 网上有没有好的 UIScrollView 教程? 2022-01-01
 - URL编码Swift iOS 2022-01-01
 - 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
 - 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
 - SetOnItemSelectedListener上的微调程序错误 2022-01-01
 
