1. <tfoot id='EQnLQ'></tfoot>

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

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

      1. 垂直居中 UILabel 时忽略 Ascender 和 Descender?

        时间:2023-09-09

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

            • <small id='KltqM'></small><noframes id='KltqM'>

              <legend id='KltqM'><style id='KltqM'><dir id='KltqM'><q id='KltqM'></q></dir></style></legend>
              <tfoot id='KltqM'></tfoot>
                  <tbody id='KltqM'></tbody>
                • 本文介绍了垂直居中 UILabel 时忽略 Ascender 和 Descender?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 AutoLayout 将一些标签放置在单元格的垂直中心.文本全部大写,但有问题的 UILabel,即使应用了 sizeToFit,也会在文本下方留出空间,这看起来很像小写 y、p 和 q 等字母的尾部.由于我是垂直居中的,这会导致偏移,这意味着文本看起来比应有的高几个像素.

                  另一个问题可能是:我可以让一个字体根据它是否包含任何使用升序或降序的字符来智能地调整它的垂直中心吗?

                  例如,字符串abbaba"不需要降序符,而字符串oyyoyo"不需要升序符.全大写的字符串也永远不需要下降器.如果我垂直居中oyyoyoyo",它会显得太低.

                  解决方案

                  感谢

                  因此,有很多方法可以对齐标签,具体取决于您是要对齐上升高度、大写高度、x 高度、基线还是下降高度.

                  假设您有一个 label 包含大写的文本,例如HELLO",并且您希望与 viewAbove 对齐以限制高度并与 viewBelow 到基线.

                  你会这样做:

                  让 font = label.font让 ascenderDelta = font.ascender - font.capHeight布局助手().addViews([标签":标签,viewAbove":viewAbove,viewBelow":viewBelow]).withMetrics(["ascenderDelta":ascenderDelta]).addConstraints([//-- 这里是对齐上限高度的约束 --"X:label.top == viewAbove.bottom - assenderDelta","X:label.baseline == viewBelow.top",...其他限制...])

                  注意:在示例中,我使用的是实用程序类 LayoutHelper,但我希望思路清晰.

                  关于自动对齐"标签:

                  我会考虑制作一个智能"标签,根据它是否包含下降、上升、大写等来调整到适当的行.

                  您可以使用 drawTextInRect 中的负插入来实现(例如 here 但是,例如, 使用 insets = {-ascenderDelta, 0, font.descender, 0}).但如果你有的话,这会裁剪任何上升/下降.我更愿意与大写对齐而不裁剪任何可能的升序.

                  I’m using AutoLayout to position some labels in the vertical centre of a cell. The text is in all-caps, but the UILabel in question, even when sizeToFit is applied, leaves space below the text, which looks a lot like it would be for the tails on letters such as a lower case y, p, and q. Since I’m centring vertically, this is causing an offset and meaning the text appears a few pixels higher than it should do.

                  Another question may be: can I have a font intelligently adjust its vertical centre dependant on whether it contains any characters which use the ascender or descender?

                  For instance, the string "abbaba" doesn’t need the descender, whereas the string "oyyoyo" doesn’t need the ascender. Strings in all-caps also never need the descender. If I vertically center "oyyoyoyo" it’ll appear too low.

                  解决方案

                  Thanks, Abhinit, for your answer.

                  I was also looking for this so I would like to post here the exact constraints you need to apply to align texts to your liking.

                  This image from Wikipedia shows the different size sections of a font.

                  So, there are many ways to align a label depending on whether you want to align to the ascender height, the cap height, the x-height, baseline or descender height.

                  Let's say you have a label containing text in caps like "HELLO" and you want to align with viewAbove to cap height and align with viewBelow to baseline.

                  You would do:

                  let font = label.font
                  let ascenderDelta = font.ascender - font.capHeight
                  
                  LayoutHelper()
                      .addViews([
                          "label":label, "viewAbove":viewAbove, "viewBelow":viewBelow
                      ])
                      .withMetrics(["ascenderDelta":ascenderDelta])
                      .addConstraints([
                  
                          // -- Here the constraints to align to cap height --
                          "X:label.top == viewAbove.bottom - ascenderDelta",
                          "X:label.baseline == viewBelow.top",
                  
                          ...other constraints...
                      ])
                  

                  Note: in the example I'm using my utility class LayoutHelper, but I hope the idea is clear.

                  About an "auto-aligning" label:

                  I will think about making an "intelligent" label that adjusts to the appropriate line depending on whether it contains descenders, ascenders, caps, etc.

                  You could do it using negative insets in drawTextInRect(like here but, for example, using insets = {-ascenderDelta, 0, font.descender, 0}). But that would crop any ascenders/descenders in case you had. I would prefer to align to caps without cropping any possible ascender.

                  这篇关于垂直居中 UILabel 时忽略 Ascender 和 Descender?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                      1. <small id='H6hiM'></small><noframes id='H6hiM'>

                          <tfoot id='H6hiM'></tfoot>
                            <tbody id='H6hiM'></tbody>