【问题标题】:Autolayout VFL and margins自动布局 VFL 和边距
【发布时间】:2014-12-13 10:43:36
【问题描述】:

在使用自动布局和使用 VFL 定义时遇到一些问题。

我在代码中有一个 UILabel 和 UIImage,我想将它们都对齐到容器的顶部。一切正常,除了 UILabel,它在顶部有我无法摆脱的填充。

我使用了一个非常简单的 VFL 字符串:“V:|-(0)-[labelView]”和“V:|-(0)-[imageView]”

imageView 可以正常工作,但 labelView 有我无法摆脱的填充。

是否有可以关闭的边距或填充设置?

任何帮助将不胜感激。

【问题讨论】:

    标签: ios autolayout margins


    【解决方案1】:

    好的,做了一些研究,感谢此链接中的图形:http://www.cocoanetics.com/2010/02/understanding-uifont/,我能够找到解决方案。

    子类 UILabel 并使用以下内容覆盖 drawTextInRect:

    - (void)drawTextInRect:(CGRect)rect
    {
       CGFloat topInset = self.font.ascender - self.font.descender - self.font.capHeight;
    
       UIEdgeInsets insets = {-topInset, 0, 0, 0};
    
       return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
    }
    

    希望这可以帮助其他希望解决类似问题的人! :-)

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 2017-03-18
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多