• <legend id='GZNWq'><style id='GZNWq'><dir id='GZNWq'><q id='GZNWq'></q></dir></style></legend>

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

      1. <tfoot id='GZNWq'></tfoot>
          <bdo id='GZNWq'></bdo><ul id='GZNWq'></ul>
        <i id='GZNWq'><tr id='GZNWq'><dt id='GZNWq'><q id='GZNWq'><span id='GZNWq'><b id='GZNWq'><form id='GZNWq'><ins id='GZNWq'></ins><ul id='GZNWq'></ul><sub id='GZNWq'></sub></form><legend id='GZNWq'></legend><bdo id='GZNWq'><pre id='GZNWq'><center id='GZNWq'></center></pre></bdo></b><th id='GZNWq'></th></span></q></dt></tr></i><div id='GZNWq'><tfoot id='GZNWq'></tfoot><dl id='GZNWq'><fieldset id='GZNWq'></fieldset></dl></div>
      2. UIButton 上的左对齐图像和居中文本

        时间:2023-07-07
          <tbody id='M6RxY'></tbody>

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

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

                  <legend id='M6RxY'><style id='M6RxY'><dir id='M6RxY'><q id='M6RxY'></q></dir></style></legend>
                1. 本文介绍了UIButton 上的左对齐图像和居中文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我看过关于右对齐的帖子,但我无法让左对齐工作.我希望按钮占据屏幕的宽度,图像在左侧,标题/文本在中间.

                  I've seen posts regarding right alignment but I can't get left-alignment to work. I want the button to take up the width of the screen, with the image on the left and the title/text in the center.

                  这不起作用(至少可靠):

                  This does not work (at least reliably):

                      button.titleLabel.textAlignment = UITextAlignmentCenter;
                      [button setImageEdgeInsets:UIEdgeInsetsMake(0, -60.0, 0, 0)];
                      button.frame = CGRectMake((self.view.frame.size.width - w ) / 2, self.view.frame.size.height - 140.0,  self.view.frame.size.width - 10.0, 40.0);
                  

                  推荐答案

                  此解决方案适用于 Swift 3,并尊重原始内容和图像边缘插入,同时保持标题标签始终位于可用空间的中心,这使得调整边距变得更加容易.

                  This solution works with Swift 3 and respects original content and image edge insets while keeping the title label always centered in the available space, which makes much easier adjusting margins.

                  它覆盖 titleRect(forContentRect:) 方法并返回正确的帧:

                  It overrides titleRect(forContentRect:) method and returns the correct frame:

                  @IBDesignable
                  class LeftAlignedIconButton: UIButton {
                      override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
                          let titleRect = super.titleRect(forContentRect: contentRect)
                          let imageSize = currentImage?.size ?? .zero
                          let availableWidth = contentRect.width - imageEdgeInsets.right - imageSize.width - titleRect.width
                          return titleRect.offsetBy(dx: round(availableWidth / 2), dy: 0)
                      }
                  }
                  

                  以下插图:

                  会导致:

                  不推荐使用之前的答案

                  这在大多数情况下都有效,但某些布局会导致 layoutSubviews 在无限循环中递归调用自身,因此谨慎使用.

                  This works in most scenarios, but some layouts cause layoutSubviews to recursively call itself in an endless loop, so use with caution.

                  @IBDesignable
                  class LeftAlignedIconButton: UIButton {
                      override func layoutSubviews() {
                          super.layoutSubviews()
                          contentHorizontalAlignment = .left
                          let availableSpace = UIEdgeInsetsInsetRect(bounds, contentEdgeInsets)
                          let availableWidth = availableSpace.width - imageEdgeInsets.right - (imageView?.frame.width ?? 0) - (titleLabel?.frame.width ?? 0)
                          titleEdgeInsets = UIEdgeInsets(top: 0, left: availableWidth / 2, bottom: 0, right: 0)
                      }
                  }
                  

                  此代码将执行相同的操作,但将图标与右边缘对齐:

                  This code would do the same but aligning the icon to the right edge:

                  @IBDesignable
                  class RightAlignedIconButton: UIButton {
                      override func layoutSubviews() {
                          super.layoutSubviews()
                          semanticContentAttribute = .forceRightToLeft
                          contentHorizontalAlignment = .right
                          let availableSpace = UIEdgeInsetsInsetRect(bounds, contentEdgeInsets)
                          let availableWidth = availableSpace.width - imageEdgeInsets.left - (imageView?.frame.width ?? 0) - (titleLabel?.frame.width ?? 0)
                          titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: availableWidth / 2)
                      }
                  }
                  

                  正确的对齐版本使用 semanticContentAttribute 所以它需要 iOS 9+.

                  The right allignment version uses semanticContentAttribute so it requires iOS 9+.

                  这篇关于UIButton 上的左对齐图像和居中文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:IOS:在uitableviewcell中保持按钮状态 下一篇:如何将 tintColor 应用于 UIImage?

                  相关文章

                      <bdo id='3BdJw'></bdo><ul id='3BdJw'></ul>

                      <tfoot id='3BdJw'></tfoot>
                    1. <legend id='3BdJw'><style id='3BdJw'><dir id='3BdJw'><q id='3BdJw'></q></dir></style></legend>

                    2. <i id='3BdJw'><tr id='3BdJw'><dt id='3BdJw'><q id='3BdJw'><span id='3BdJw'><b id='3BdJw'><form id='3BdJw'><ins id='3BdJw'></ins><ul id='3BdJw'></ul><sub id='3BdJw'></sub></form><legend id='3BdJw'></legend><bdo id='3BdJw'><pre id='3BdJw'><center id='3BdJw'></center></pre></bdo></b><th id='3BdJw'></th></span></q></dt></tr></i><div id='3BdJw'><tfoot id='3BdJw'></tfoot><dl id='3BdJw'><fieldset id='3BdJw'></fieldset></dl></div>
                    3. <small id='3BdJw'></small><noframes id='3BdJw'>