• <tfoot id='gXOOG'></tfoot>

    <small id='gXOOG'></small><noframes id='gXOOG'>

    <i id='gXOOG'><tr id='gXOOG'><dt id='gXOOG'><q id='gXOOG'><span id='gXOOG'><b id='gXOOG'><form id='gXOOG'><ins id='gXOOG'></ins><ul id='gXOOG'></ul><sub id='gXOOG'></sub></form><legend id='gXOOG'></legend><bdo id='gXOOG'><pre id='gXOOG'><center id='gXOOG'></center></pre></bdo></b><th id='gXOOG'></th></span></q></dt></tr></i><div id='gXOOG'><tfoot id='gXOOG'></tfoot><dl id='gXOOG'><fieldset id='gXOOG'></fieldset></dl></div>

        <bdo id='gXOOG'></bdo><ul id='gXOOG'></ul>
      <legend id='gXOOG'><style id='gXOOG'><dir id='gXOOG'><q id='gXOOG'></q></dir></style></legend>

        UIButton中图像下的标签

        时间:2023-07-09

        <small id='TLGOn'></small><noframes id='TLGOn'>

        <i id='TLGOn'><tr id='TLGOn'><dt id='TLGOn'><q id='TLGOn'><span id='TLGOn'><b id='TLGOn'><form id='TLGOn'><ins id='TLGOn'></ins><ul id='TLGOn'></ul><sub id='TLGOn'></sub></form><legend id='TLGOn'></legend><bdo id='TLGOn'><pre id='TLGOn'><center id='TLGOn'></center></pre></bdo></b><th id='TLGOn'></th></span></q></dt></tr></i><div id='TLGOn'><tfoot id='TLGOn'></tfoot><dl id='TLGOn'><fieldset id='TLGOn'></fieldset></dl></div>
        <tfoot id='TLGOn'></tfoot>

            <bdo id='TLGOn'></bdo><ul id='TLGOn'></ul>

                  <tbody id='TLGOn'></tbody>
                <legend id='TLGOn'><style id='TLGOn'><dir id='TLGOn'><q id='TLGOn'></q></dir></style></legend>

                  本文介绍了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中图像下的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Swift 中 UIButton 的圆顶角 下一篇:UIButton 无法在 iPhone 屏幕的底部区域正确注册触摸

                  相关文章

                    <small id='JS5DI'></small><noframes id='JS5DI'>

                  1. <tfoot id='JS5DI'></tfoot>
                      <bdo id='JS5DI'></bdo><ul id='JS5DI'></ul>

                      <i id='JS5DI'><tr id='JS5DI'><dt id='JS5DI'><q id='JS5DI'><span id='JS5DI'><b id='JS5DI'><form id='JS5DI'><ins id='JS5DI'></ins><ul id='JS5DI'></ul><sub id='JS5DI'></sub></form><legend id='JS5DI'></legend><bdo id='JS5DI'><pre id='JS5DI'><center id='JS5DI'></center></pre></bdo></b><th id='JS5DI'></th></span></q></dt></tr></i><div id='JS5DI'><tfoot id='JS5DI'></tfoot><dl id='JS5DI'><fieldset id='JS5DI'></fieldset></dl></div>

                      <legend id='JS5DI'><style id='JS5DI'><dir id='JS5DI'><q id='JS5DI'></q></dir></style></legend>