How to align baselines of text in UILabels with different font sizes on iOS?

前端 未结 5 1880
野性不改
野性不改 2021-01-04 01:13

I need to align the baselines of text in UILabels. What I\'m currently doing is I\'m aligning the baselines of UILabels containing the text, and when the text font size in t

5条回答
  •  一个人的身影
    2021-01-04 01:50

    I was looking to do this myself (just now) and found my answer on an almost identical question. It's not simple solution though, we have to do the math.

    I only needed to do it with 2 different labels and I'm doing it in a subclass of UIView.

    - (void)layoutSubviews {
        [majorLabel sizeToFit];
        [minorLabel sizeToFit];
    
        CGRect changedFrame = minorLabel.frame;
        changedFrame.origin.x = majorLabel.frame.size.width;
        changedFrame.origin.y = (majorLabel.frame.size.height + majorLabel.font.descender) - (minorLabel.frame.size.height + minorLabel.font.descender);
        minorLabel.frame = changedFrame;
    }
    

提交回复
热议问题