在iOS中 - 如何在不改变其位置的情况下将UILabel放入其文本中? [英] In iOS - How do I fit a UILabel to its text, without changing its position?
问题描述
我在 UILabel
上调用 sizeToFit
,其中包含右对齐文本。它缩小了 UILabel
的高度和宽度,并使文本适合 UILabel
的左上角。
I'm calling sizeToFit
on a UILabel
which has right aligned text in it. It shrinks the height and width of the UILabel
and fits the text to the top left of the UILabel
.
现在...... UILabel
的位置不正确。如何让 UILabel
保持原始位置(右对齐)或移动它以使其显示在原始位置?
Now...the position of the UILabel
is incorrect. How can I make the UILabel
stay in its original position (right aligned) or move it so it'll appear at its original position?
再一次 - 问题是 sizeToFit
方法正在缩小 UILabel $ c右侧的宽度$ C>。它将
UILabel
文本视为左对齐。但我的文字是正确对齐的。因此, UILabel
的右侧边框是我的文字开头的地方。
Once again - the problem is that the sizeToFit
method is shrinking the width from the right side of the UILabel
. It is treating the UILabel
text as left aligned. But my text is right aligned. So, the right side border of the UILabel
is where my text begins.
推荐答案
你检查之前和之前的宽度之后,然后将视图的中心向右移动差值/ 2.
You check the widths before & after, then move the center of the view right by the difference / 2.
顺便说一下,我认为盒子(框架)在两个方向都缩小了,保持中心完整。
By the way, I think the "box" (frame) is shrunk in both directions, keeping the center intact.
这篇关于在iOS中 - 如何在不改变其位置的情况下将UILabel放入其文本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!