有没有办法确保文本元素真正与垂直中间对齐?

发布于 2024-12-04 15:59:40 字数 1793 浏览 1 评论 0原文

我过去经常遇到这个问题,到目前为止还没有找到通用的好解决方案。

本质上,问题是这样的。如果我想在容器中垂直对齐内容,我将使用 verticalAlign="middle",但这并不能完全解决真正垂直对齐文本的问题。文本在垂直方向上总是太高,因此在过去,我只是调整了 paddingToppaddingBottom 进行补偿,但这不能用作 的字体_sans 因操作系统而异。

给出以下布局代码,您将看到问题:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">

    <s:Group width="100" height="50">
        <s:Group width="100%" height="100%">
            <s:Rect width="100%" height="100%" radiusX="5" radiusY="5">
                <s:fill>
                    <s:SolidColor color="#333333"/>
                </s:fill>
            </s:Rect>
        </s:Group>

        <s:VGroup width="100%" height="100%" 
                paddingTop="10" paddingBottom="10"
                paddingRight="10" paddingLeft="10"
                verticalAlign="middle">
            <s:Line width="100%">
                <s:stroke>
                    <s:SolidColorStroke color="#CCCCCC" weight="1"/>
                </s:stroke>
            </s:Line>
        </s:VGroup>

        <s:Label text="100 Percent!" top="10" bottom="10" 
                left="10" right="10" verticalAlign="middle"
                textAlign="center" color="#FFFFFF"/>
    </s:Group> 

    <s:layout>
        <s:VerticalLayout horizontalAlign="center"
                verticalAlign="middle"/>
    </s:layout>
</s:Application>

发生的情况非常清楚。水平线与框的正中间对齐,但文本偏移了任意数量。我认为这是由于文本基线对齐或其他原因造成的。无论字体大小和字体如何,有没有办法解决这个问题?我想让该行基本上穿过文本的中间,忽略文本逻辑行下方可能需要的额外空间(即:在布局时忽略“g's”和“j's”的底部)。

I've often had this problem in the past and to this date haven't found a good solution that works universally.

Essentially, the problem is this. If I want to align things vertically in a container, I'll use verticalAlign="middle", but this doesn't exactly solve the problem of truly aligning text vertically. Text is always too high vertically, so in the past, I've just adjusted paddingTop and paddingBottom to compensate, but that doesn't work as the font for _sans differs from operating system to operating system.

Given the following layout code, you'll see the problem:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">

    <s:Group width="100" height="50">
        <s:Group width="100%" height="100%">
            <s:Rect width="100%" height="100%" radiusX="5" radiusY="5">
                <s:fill>
                    <s:SolidColor color="#333333"/>
                </s:fill>
            </s:Rect>
        </s:Group>

        <s:VGroup width="100%" height="100%" 
                paddingTop="10" paddingBottom="10"
                paddingRight="10" paddingLeft="10"
                verticalAlign="middle">
            <s:Line width="100%">
                <s:stroke>
                    <s:SolidColorStroke color="#CCCCCC" weight="1"/>
                </s:stroke>
            </s:Line>
        </s:VGroup>

        <s:Label text="100 Percent!" top="10" bottom="10" 
                left="10" right="10" verticalAlign="middle"
                textAlign="center" color="#FFFFFF"/>
    </s:Group> 

    <s:layout>
        <s:VerticalLayout horizontalAlign="center"
                verticalAlign="middle"/>
    </s:layout>
</s:Application>

What happens is pretty clear. The horizontal line gets aligned to the exact middle of the box, but the text is offset by some arbitrary amount. I assume that this is because of the text baseline alignment or something. Is there a way to fix this regardless of the font size and face? I'd like to have the line essentially go right through the middle of the text, ignoring the extra space possibly needed for below the logical line of text (ie: ignore the bottom part of "g's" and "j's" when laying things out).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-12-11 15:59:40

查看布局限制。您还可以研究一个简单的绑定表达式,以便他们即时计算确切的需求。尽管它们通常是完美的。

他们还很好地响应了调整大小事件气泡。

Look into layout constraints. You could also look into a simple binding expression for them to calculate the exact needs on the fly. Although they are generally perfect as is.

They also respond to the resize event bubble nicely.

夜空下最亮的亮点 2024-12-11 15:59:40

尝试:

<s:Group width="100" height="50">
   <s:Line width="100%" verticalCenter="0">
      <s:stroke>
         <s:SolidColorStroke color="#CCCCCC" weight="1"/>
      </s:stroke>
   </s:Line>

   <s:Label text="100 Percent!" horizontalCenter="0" verticalCenter="0"/>
</s:Group>

Try:

<s:Group width="100" height="50">
   <s:Line width="100%" verticalCenter="0">
      <s:stroke>
         <s:SolidColorStroke color="#CCCCCC" weight="1"/>
      </s:stroke>
   </s:Line>

   <s:Label text="100 Percent!" horizontalCenter="0" verticalCenter="0"/>
</s:Group>
放低过去 2024-12-11 15:59:40

显然不是,尽管可能存在定制解决方案。

Evidently not, though a custom solution might exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文