如何压缩 UILabel 顶部和底部的额外填充 [英] How to compress extra padding at the top and bottom of the UILabel

查看:25
本文介绍了如何压缩 UILabel 顶部和底部的额外填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看附加的屏幕截图,当文本内容较少时,我看到 UILabel 的顶部和底部添加了额外的填充.

see the attached screenshot , I am seeing an extra padding getting added the top and bottom of the UILabel when the text content is less.

但是当 dummyDescription Label 文本更多时,效果也很好.仅当 dummyDescriptionLabel 较少时才会发生问题.

But works as perfectly when dummyDescription Label text is more. Issue is happening only when dummyDescriptionLabel is less.

以下是我以编程方式添加的约束

Below are my constraints which are added progrmatically

NSLayoutConstraint.activate([
            self.dummyImageView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 24),
            self.dummyImageView.topAnchor.constraint(equalTo: self.topAnchor,constant:24),
            self.dummyImageView.widthAnchor.constraint(equalToConstant: 105),
            self.dummyImageView.heightAnchor.constraint(equalToConstant: 67),

            self.dummyNameLabel.topAnchor.constraint(equalTo: self.dummyImageView.topAnchor),
            self.dummyNameLabel.leadingAnchor.constraint(equalTo: self.dummyImageView.trailingAnchor, constant: 16),
            self.dummyNameLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -24),

            self.dummyDescriptionLabel.topAnchor.constraint(equalTo: self.dummyImageView.bottomAnchor, constant: 16),
            self.dummyDescriptionLabel.leadingAnchor.constraint(equalTo: self.dummyImageView.leadingAnchor),
            self.dummyDescriptionLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -24),


            self.dummyButton.topAnchor.constraint(equalTo: self.dummyDescriptionLabel.bottomAnchor, constant: 5),
            self.dummyButton.leadingAnchor.constraint(equalTo: self.dummyImageImageView.leadingAnchor),
            self.readMoreButton.heightAnchor.constraint(equalToConstant: 20)
        ])

我也设置了下面的仍然没有运气

I have also set below ones still no luck

self.dummyNameLabel.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .horizontal)
self.dummyDescriptionLabel.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .horizontal)

任何帮助将不胜感激.

推荐答案

应该为垂直约束设置内容 Hugging Priority.即

The content Hugging Priority should be set for vertical constraints. i.e

   self.dummyNameLabel.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .horizontal)
   self.dummyDescriptionLabel.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .horizontal)

应替换为:

    self.dummyNameLabel.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .vertical)
    self.dummyDescriptionLabel.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .vertical)

这样,标签的边框将始终与内在内容的高度相同.

In this way, the frame of the labels will always be the same as the intrinsic content's height.

同样,为了防止标签内容被边界裁剪,您可以将垂直压缩优先级设置为默认高.

Similarly, to prevent the content of the labels from being clipped by their bounds, you can set the vertical compression priority to a default high.

这篇关于如何压缩 UILabel 顶部和底部的额外填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆