• <tfoot id='nsshJ'></tfoot>

      <i id='nsshJ'><tr id='nsshJ'><dt id='nsshJ'><q id='nsshJ'><span id='nsshJ'><b id='nsshJ'><form id='nsshJ'><ins id='nsshJ'></ins><ul id='nsshJ'></ul><sub id='nsshJ'></sub></form><legend id='nsshJ'></legend><bdo id='nsshJ'><pre id='nsshJ'><center id='nsshJ'></center></pre></bdo></b><th id='nsshJ'></th></span></q></dt></tr></i><div id='nsshJ'><tfoot id='nsshJ'></tfoot><dl id='nsshJ'><fieldset id='nsshJ'></fieldset></dl></div>

      <small id='nsshJ'></small><noframes id='nsshJ'>

      • <bdo id='nsshJ'></bdo><ul id='nsshJ'></ul>

      <legend id='nsshJ'><style id='nsshJ'><dir id='nsshJ'><q id='nsshJ'></q></dir></style></legend>
      1. 如何在 iOS 上将 UILabel 中的文本基线与不同的字体大小对齐?

        时间:2023-10-22
        <i id='p1UhL'><tr id='p1UhL'><dt id='p1UhL'><q id='p1UhL'><span id='p1UhL'><b id='p1UhL'><form id='p1UhL'><ins id='p1UhL'></ins><ul id='p1UhL'></ul><sub id='p1UhL'></sub></form><legend id='p1UhL'></legend><bdo id='p1UhL'><pre id='p1UhL'><center id='p1UhL'></center></pre></bdo></b><th id='p1UhL'></th></span></q></dt></tr></i><div id='p1UhL'><tfoot id='p1UhL'></tfoot><dl id='p1UhL'><fieldset id='p1UhL'></fieldset></dl></div>
          <bdo id='p1UhL'></bdo><ul id='p1UhL'></ul>

          <small id='p1UhL'></small><noframes id='p1UhL'>

            <tbody id='p1UhL'></tbody>
          <tfoot id='p1UhL'></tfoot>
        • <legend id='p1UhL'><style id='p1UhL'><dir id='p1UhL'><q id='p1UhL'></q></dir></style></legend>
                1. 本文介绍了如何在 iOS 上将 UILabel 中的文本基线与不同的字体大小对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要对齐 UILabel 中文本的基线.我目前正在做的是对齐包含文本的 UILabels 的基线,当两个标签中的文本字体大小不同时,这会导致对齐的 UILabels 基线但未对齐的文本基线(未对齐一小部分,但仍然错位).标签包含在自定义 UIView 子类中,因此 self 指的是包含 UIView.

                  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 two labels is different, this results in aligned UILabels baseline but misaligned text baseline (misaligned by a small margin, but still misaligned). The labels are included in a custom UIView subclass, therefore self refers to the encompassing UIView.

                  这里是相关代码

                  [self.mySmallLabel sizeToFit];
                  [self.myBigLabel sizeToFit];
                  
                  self.mySmallLabel.frame = CGRectMake(0,     
                                                       self.bounds.size.height - self.mySmallLabel.bounds.size.height, 
                                                       self.mySmallLabel.bounds.size.width, 
                                                       self.mySmallLabel.bounds.size.height);
                  
                  self.myBigLabel.frame = CGRectMake(self.mySmallLabel.frame.origin.x + self.mySmallLabel.bounds.size.width, 
                                                     self.bounds.size.height - self.myBigLabel.bounds.size.height, 
                                                     self.myBigLabel.bounds.size.width, 
                                                     self.myBigLabel.bounds.size.height);
                  [self.mySmallLabel sizeToFit];
                  [self.myBigLabel sizeToFit];
                  

                  此代码导致下面链接的图像中的对齐.

                  This code results in the aligment in the image linked below.

                  如您所见,即使 UILabel 的基线是对齐的,文本的基线也会有一点点未对齐.如何动态对齐文本的基线(因为字体大小可能会在运行时发生变化)?

                  As you can see, even though the UILabel baselines are aligned, the baselines of the text is misaligned by a small margin. How can I align the baselines of text dynamically (because font sizes might change at runtime)?

                  推荐答案

                  我在几次使用 this answer不同的地方,但基线有时在 Retina 显示器上相差一个像素.下面的代码段说明了屏幕的比例:

                  I was using this answer in a couple of different places, but the baselines were sometimes a pixel off on Retina displays. The snippet below accounts for the screen’s scale:

                  [majorLabel sizeToFit];
                  [minorLabel sizeToFit];
                  
                  CGRect changedFrame = minorLabel.frame;
                  changedFrame.origin.x = CGRectGetWidth(majorLabel.frame);
                  
                  const CGFloat scale = [UIScreen mainScreen].scale;
                  const CGFloat majorLabelBaselineInSuperView = CGRectGetMaxY(majorLabel.frame) + majorLabel.font.descender;
                  const CGFloat minorLabelBaselineInOwnView = CGRectGetHeight(minorLabel.frame) + minorLabel.font.descender;
                  changedFrame.origin.y = ceil((majorLabelBaselineInSuperView - minorLabelBaselineInOwnView) * scale) / scale;
                  
                  minorLabel.frame = changedFrame;
                  

                  这篇关于如何在 iOS 上将 UILabel 中的文本基线与不同的字体大小对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从服务器下载音乐文件并保存在我的应用程序中 下一篇:iOS SDK UIViewContentModeScaleAspectFit 与 UIViewContentModeS

                  相关文章

                  • <bdo id='oOjdR'></bdo><ul id='oOjdR'></ul>

                    <i id='oOjdR'><tr id='oOjdR'><dt id='oOjdR'><q id='oOjdR'><span id='oOjdR'><b id='oOjdR'><form id='oOjdR'><ins id='oOjdR'></ins><ul id='oOjdR'></ul><sub id='oOjdR'></sub></form><legend id='oOjdR'></legend><bdo id='oOjdR'><pre id='oOjdR'><center id='oOjdR'></center></pre></bdo></b><th id='oOjdR'></th></span></q></dt></tr></i><div id='oOjdR'><tfoot id='oOjdR'></tfoot><dl id='oOjdR'><fieldset id='oOjdR'></fieldset></dl></div>
                  1. <small id='oOjdR'></small><noframes id='oOjdR'>

                  2. <tfoot id='oOjdR'></tfoot>
                    <legend id='oOjdR'><style id='oOjdR'><dir id='oOjdR'><q id='oOjdR'></q></dir></style></legend>