有什么方法可以将 UILabels 与不同的字体底部对齐吗?

发布于 2024-10-09 12:39:43 字数 538 浏览 0 评论 0原文

我在自定义 UITableViewCell 的 contentView 中有一个 UILabels 数组。每个标签的字体大小通过排名来形成标签云。在设置单元格(行)的方法中,我迭代适合该行的单词对象,为每个 UILabel 设置框架,如下所示:

CGRect theFrame = CGRectMake(xPosAdjuster,
    theWordRow.rowHeight - thisWord.lblHeight,
    thisWord.lblWidth,
    thisWord.lblHeight);
UILabel *myLabel = [[UILabel alloc] initWithFrame:theFrame];

这使标签的框架对齐(参见下图),但是不幸的是,标签的填充是字体大小的函数。

alt text

有没有办法删除 UILabel 上的填充(边框)和/或精确计算它以便我可以调整相应的帧的 y 位置?

谢谢

I have an array of UILabels inside the contentView of a custom UITableViewCell. The font of each label is sized by ranking to form a tag cloud. In the method that sets up the cell (row), I iterate through the word objects that will fit on that line, setting up the frame for each UILabel as follows:

CGRect theFrame = CGRectMake(xPosAdjuster,
    theWordRow.rowHeight - thisWord.lblHeight,
    thisWord.lblWidth,
    thisWord.lblHeight);
UILabel *myLabel = [[UILabel alloc] initWithFrame:theFrame];

This gets the frames of the labels aligned (see image below), but, unfortunately, the labels have a padding that is a function of the font size.

alt text

Is there any way to remove the padding (border) on a UILabel and/or calculate it exactly so I can adjust the y pos of the frames accordingly?

Thanks

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

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

发布评论

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

评论(2

極樂鬼 2024-10-16 12:39:43

这是我排列标签的最终代码:

CGRect theFrame = CGRectMake(xPosAdjuster,
    floor(theWordRow.rowHeight - thisWord.lblHeight),
    floor(thisWord.lblWidth),
    thisWord.lblHeight);
UILabel *myLabel = [[UILabel alloc] initWithFrame:theFrame];
...
CGRect newFrame = myLabel.frame;
newFrame.origin.y -= floor(myLabel.font.descender);
myLabel.frame = newFrame;

alt text

Here is my final code that lines up the labels:

CGRect theFrame = CGRectMake(xPosAdjuster,
    floor(theWordRow.rowHeight - thisWord.lblHeight),
    floor(thisWord.lblWidth),
    thisWord.lblHeight);
UILabel *myLabel = [[UILabel alloc] initWithFrame:theFrame];
...
CGRect newFrame = myLabel.frame;
newFrame.origin.y -= floor(myLabel.font.descender);
myLabel.frame = newFrame;

alt text

原谅我要高飞 2024-10-16 12:39:43

您可能需要查看此页面。苹果文档上有信息,但这是我发现的第一个。

因此,您似乎必须根据 UIFont 的下降部分进行一些计算。您可以轻松获取此值,它被定义为 UIFont 上的属性。

You may want to take a look at this page. There is information on Apple's Docs, however this was the first I found.

So it looks like you'll have to do some calculation based on the descender of the UIFont. You can easily get this value, it is defined as a property on UIFont.

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