首页 文章

UILabels用不同的字体底部对齐的方法是什么?

提问于
浏览
8

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

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

这会使标签的帧对齐(请参见下图),但不幸的是,标签的填充是字体大小的函数 .

alt text

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

谢谢

2 回答

  • 7

    你可能想看一下this page . 有关于Apple的文档的信息,但这是我发现的第一个 .

    所以看起来你必须根据 UIFont 的下降进行一些计算 . 您可以轻松获取此值,它被定义为 UIFont 上的属性 .

  • 10

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

    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

相关问题