确定使用adjustFontSizeToFitWidth时居中的UILabel高度
我有以下单元设计,其中数字标签缩小且“Overall”标签直接位于下方。确定使用adjustFontSizeToFitWidth时居中的UILabel高度
我已经设定了正确的adjustFontSizeToFitWidth
和minimumFontSize
性能。字体正确调整大小。但是,将数字标签固定在底部是具有挑战性的。特别是当字体缩小时,两个标签之间的间距变宽并且不会出现垂直居中。
我试过sizeToFit
,sizeThatFits
,并使用字体的pointSize
。所有失败。
我知道sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
,但不明白为什么我需要它与adjustFontSizeToFitWidth
结合使用。
啊,所以你想将UILabels
定位在容器视图的中间(水平和垂直)?
我已经改写了我的答案,所以对未来的读者会更有意义。
我的代码是假设你有3个IBOutlets设置:
UIView *containerView; //Your nice view containing the two textfields.
UILabel *points; //The label containing the number.
UILabel *overall; //The textfield containing the text 'overall'.
你可以简单地设置标签的框架后分配文本并调用sizeToFit
。
-
该第一线定位所述
UILabel
点,在于y坐标的唯一变化是containerView
减去一半的本身的高度的一半。points.frame = CGRectMake(points.frame.origin.x, (containerView.frame.size.height/2) - (points.frame.size.height/2), points.frame.size.width, points.frame.size.height);
-
要定位
overall
相应的 - 说有说的数和总的标签6之间的距离:int space = 6; overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height);
-
看了您的意见,我觉得你是这个解决方案之后。如果你想让
UILabels
出现在中间;从points
像这样的y值(具有号为2的代码在其下方)减去(overall.frame.size.height/2) + (space/2)
:int space = 6; points.frame = CGRectMake(points.frame.origin.x, ((containerView.frame.size.height/2) - (points.frame.size.height/2)) - ((overall.frame.size.height/2) + (space/2)), points.frame.size.width, points.frame.size.height); overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height);
最后一点会产生这样图像的输出。正如你所看到的那样,蓝线是整个图像的一半,并且在中点与黑色矩形(它紧贴两个标签)相交。我希望这是你以后的样子。
当你调用它'squareView'时,容器视图可能是他们要走的路。 – 2013-02-14 15:50:04
我认为你有一个总分的观点,有这个好背景和右边的点。我应该把它重新命名为任何东西。它现在是'containerView' - 好多了! :) – Patrick 2013-02-14 16:07:13
而不是使用两个标签,使用CATextLayer
代替。你会很容易使一部分BOLD和其他正常。相对于放置两个标签,加上位置和调整一个图层的大小将很容易。阴影设置,换行符模式,字体你将能够调整一切美丽:)
我会记住未来。但在这一点上,所有东西都是作为UILabel构建的,我宁愿不重构。加上它现在是一个挑战:) – 2013-02-14 18:33:26
你试过'sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:'? – 2013-02-14 15:09:36
我已经考虑过了。查看更新。 – 2013-02-14 15:11:18
你能改说你的问题吗?我想我可以帮助你,但我不确定你确切的问题是什么。看起来你需要做的就是在改变“123”标签的大小后,改变“123”和“整体”标签的框架。 – FreeAsInBeer 2013-02-14 15:15:24