I have a problem with UILabel subclass cutting off text in the bottom. Label is of proper height to fit the text, there is some space left in the bottom, but the text is still being cut off.
我有一个问题,UILabel子类切断底部的文本。标签的高度适合文本,底部留有一些空间,但文本仍然被剪掉。
The red stripes are border added to label's layer.
红色条纹是添加到标签图层的边框。
I subclass the label to add edge insets.
我将标签子类化以添加边缘插入。
override func sizeThatFits(size: CGSize) -> CGSize {
var size = super.sizeThatFits(size)
size.width += insets.left + insets.right
size.height += insets.top + insets.bottom
return size
}
override func drawTextInRect(rect: CGRect) {
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}
However, in this particular case the insets are zero.
但是,在这种特殊情况下,插入是零。
4 个解决方案
#1
8
Turns out the problem was with
事实证明问题在于
self.lineBreakMode = .ByClipping
changing it to
改变它
self.lineBreakMode = .ByCharWrapping
Solved the problem
解决了这个问题
#2
1
Happened for me when providing topAnchor and centerYAnchor for label at the same time. Leaving just one anchor fixed the problem.
在为同时提供topAnchor和centerYAnchor标签时发生的事情发生了。只留下一个锚就解决了这个问题。
#3
1
My problem was that the label's (vertical) content compression resistance priority was not high enough; setting it to required (1000) fixed it.
我的问题是标签的(垂直)内容压缩阻力优先级不够高;将其设置为必需(1000)修复它。
It looks like the other non-OP answers may be some sort of workaround for this same underlying issue.
看起来其他非OP答案可能是针对同一个潜在问题的某种解决方法。
#4
0
Other answers didn't help me, but what did was constraining the height of the label to whatever height it needed, like so:
其他答案对我没有帮助,但是将标签的高度限制在所需的任何高度,例如:
let unconstrainedSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
label.heightAnchor.constraint(equalToConstant: label.sizeThatFits(unconstrainedSize).height).isActive = true
Also, sizeThatFits(_:)
will return a 0 by 0
size if your label's text
field is nil
or equal to ""
此外,如果标签的文本字段为nil或等于“”,sizeThatFits(_ :)将返回0 x大小
#1
8
Turns out the problem was with
事实证明问题在于
self.lineBreakMode = .ByClipping
changing it to
改变它
self.lineBreakMode = .ByCharWrapping
Solved the problem
解决了这个问题
#2
1
Happened for me when providing topAnchor and centerYAnchor for label at the same time. Leaving just one anchor fixed the problem.
在为同时提供topAnchor和centerYAnchor标签时发生的事情发生了。只留下一个锚就解决了这个问题。
#3
1
My problem was that the label's (vertical) content compression resistance priority was not high enough; setting it to required (1000) fixed it.
我的问题是标签的(垂直)内容压缩阻力优先级不够高;将其设置为必需(1000)修复它。
It looks like the other non-OP answers may be some sort of workaround for this same underlying issue.
看起来其他非OP答案可能是针对同一个潜在问题的某种解决方法。
#4
0
Other answers didn't help me, but what did was constraining the height of the label to whatever height it needed, like so:
其他答案对我没有帮助,但是将标签的高度限制在所需的任何高度,例如:
let unconstrainedSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
label.heightAnchor.constraint(equalToConstant: label.sizeThatFits(unconstrainedSize).height).isActive = true
Also, sizeThatFits(_:)
will return a 0 by 0
size if your label's text
field is nil
or equal to ""
此外,如果标签的文本字段为nil或等于“”,sizeThatFits(_ :)将返回0 x大小